• NPOI导出Excel封装


    直接上代码

        public class ExcelUtils
        {
            public static ICellStyle CreateStyle(IWorkbook workbook,
                string fontName = "宋体",
                int fontSize = 10,
                bool isBold = false,
                HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment verticalAlignment = VerticalAlignment.Center,
                bool wrapText = false,
                BorderStyle left = BorderStyle.Thin,
                BorderStyle right = BorderStyle.Thin,
                BorderStyle top = BorderStyle.Thin,
                BorderStyle bottom = BorderStyle.Thin
                )
            {
                IFont font = workbook.CreateFont();
                font.FontName = fontName;
                font.FontHeightInPoints = (short)fontSize;
                font.IsBold = isBold;
                ICellStyle style = workbook.CreateCellStyle();
                style.SetFont(font);
                style.Alignment = horizontalAlignment;
                style.VerticalAlignment = verticalAlignment;
                style.WrapText = wrapText;
                style.BorderLeft = left;
                style.BorderRight = right;
                style.BorderTop = top;
                style.BorderBottom = bottom;
                return style;
            }
        }
    
        public class ExcelRow
        {
            /// <summary>
            /// 
            /// </summary>
            public ISheet Sheet { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public IRow Row { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int RowIndex { get; private set; }
    
            public int ColumnIndex { get; set; }
    
            public const int HeightConstant = 20;
    
            public ExcelRow(ISheet sheet, int height = 20)
            {
                if (sheet.PhysicalNumberOfRows == 0)
                {
                    this.RowIndex = 0;
                }
                else
                {
                    this.RowIndex = sheet.PhysicalNumberOfRows ;
                }
                this.ColumnIndex = 0;
                this.Sheet = sheet;
                this.Row = this.Sheet.CreateRow(this.RowIndex);
                this.Row.Height = (short)(HeightConstant * height);
            }
    
    
        }
    
        public class ExcelCell
        {
            /// <summary>
            /// 
            /// </summary>
            public ExcelRow Row { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public ICell Cell { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int ColumnIndex { get; set; }
    
            public const int WidthConstant = 256;
            public ExcelCell(ExcelRow row, ICellStyle style, int width = 10)
            {
                this.Row = row;
                this.ColumnIndex = this.Row.ColumnIndex++;
    
                this.Cell = this.Row.Row.CreateCell(this.ColumnIndex, CellType.Blank);
                this.Row.Sheet.SetColumnWidth(this.ColumnIndex, width * WidthConstant);
                this.Cell.CellStyle = style;
    
            }
    
    
            public ExcelCell SetValue(string value)
            {
                this.Cell.SetCellValue(value);
                this.Cell.SetCellType(CellType.String);
                return this;
            }
    
            public ExcelCell SetValue(bool value)
            {
                this.Cell.SetCellValue(value);
                this.Cell.SetCellType(CellType.Boolean);
                return this;
            }
    
            public ExcelCell SetValue(double value)
            {
                this.Cell.SetCellValue(value);
                this.Cell.SetCellType(CellType.Numeric);
                return this;
            }
    
            public ExcelCell SetValue(DateTime value)
            {
                this.Cell.SetCellValue(value);
                return this;
            }
    
            public ExcelCell SetRowSpan(int rowspan)
            {
                this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex + rowspan - 1, this.ColumnIndex, this.ColumnIndex));
                return this;
            }
    
            public ExcelCell SetColSpan(int colspan)
            {
                this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex, this.ColumnIndex, this.ColumnIndex + colspan - 1));
                return this;
            }
    
    
        }

    调用方法:

      MemoryStream stream = new MemoryStream();
                IWorkbook workbook = new HSSFWorkbook();
                ICellStyle headStyle = ExcelUtils.CreateStyle(workbook, "宋体", 10, true);
                ICellStyle bodyStyle = ExcelUtils.CreateStyle(workbook, "宋体", 10, false);
    
    
                ExcelRow tr = null;
                ExcelCell td = null;
          
    
                ISheet sheet = workbook.CreateSheet("Sheet0");
    
                tr = new ExcelRow(sheet);
                td = new ExcelCell(tr, headStyle);
                td.SetValue("序号");
    
                td = new ExcelCell(tr, headStyle);
                td.SetValue("姓名");
    
                td = new ExcelCell(tr, headStyle);
                td.SetValue("金额");
    
                for (int i = 0; i < 10; i++)
                {
                    tr = new ExcelRow(sheet);
                    td = new ExcelCell(tr, bodyStyle);
                    td.SetValue(i + 1);
    
                    td = new ExcelCell(tr, bodyStyle);
                    td.SetValue("姓名");
    
                    td = new ExcelCell(tr, bodyStyle);
                    td.SetValue(new Random().Next(1, 100));
                }
    
                td.SetColSpan(3);
    
                //fs.Flush();
                // fs.Close();
                workbook.Write(stream);//将Excel写入流
                stream.Flush();
                stream.Position = 0;
    
    
                File.WriteAllBytes("a.xls", stream.GetBuffer());
    
            
                Console.Read();
  • 相关阅读:
    使用jmeter进行接口测试
    jenkins默认插件
    【机器学习】关于PCA 昕
    雲瑞文件直链分享软件
    FOB离岸价
    【数据分享】某地区1959~2019年60年降雨量时间序列数据
    【数据分享】银行客户流失Bank Customer Churn数据
    【数据分享】糖尿病患者研究数据集
    【数据分享】维基百科Wiki负面有害评论(网络暴力)文本数据多标签分类挖掘可视化
    C# Speech
  • 原文地址:https://www.cnblogs.com/shya/p/8922181.html
Copyright © 2020-2023  润新知