• C#npoi导出excel一些自己的看法


    之前转过一篇类似的文章,那个是用C#自带的excel类操作Excel的,还有一种在c#上操作excel的方法便是npoi了,npoi是poi的C#版本。

    npoi操作excel有两种形式,一种是hssf和xssf。hssf是用来生成excel2003之前的版本,生成的excel后缀是“.xls”,而xssf是excel2007的版本,操作的excel的后缀是“.xlsx”。

    npoi中常用的类型:

    XSSF/HSSFWorkbook:excel文件的工作簿,

    Isheet:excel文件的工作簿中的工作表,

    IRow:excel文件的工作簿中的工作表中的行,

    ICell:excel文件的工作簿中的工作表中的行中的单元格。

    举一个简单的导出excel的方法

     public static IWorkbook TableToExcel(DataTable dt, string fileExt = ".xlsx")
            {
                IWorkbook workbook;
                if (fileExt == ".xlsx")
                {
                    workbook = new XSSFWorkbook();
                }
                else if (fileExt == ".xls")
                {
                    workbook = new HSSFWorkbook();
                }
                else
                {
                    workbook = null;
                }
                if (workbook == null)
                {
                    return null;
                }
                ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);
    
                //表头  
                IRow row = sheet.CreateRow(0);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    ICell cell = row.CreateCell(i);
                    cell.SetCellValue(dt.Columns[i].ColumnName);
    
                }
    
                //数据  
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    IRow row1 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ICell cell = row1.CreateCell(j);
                        cell.SetCellValue(dt.Rows[i][j].ToString());
    
                    }
                }
                //设置宽度
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    int length = Encoding.Default.GetBytes(row.GetCell(i).ToString()).Length;
                    sheet.SetColumnWidth(i, (length + 2) * 256);
                }
                return workbook;
    
    
            }

    //生成excel:

    using (MemoryStream ms = new MemoryStream())
    {
    workbook.Write(ms);
    var buffer = ms.GetBuffer();
    ms.Close();
    return File(buffer, "application/vnd.ms-excel", "个人提成补录模板.xls");
    }

     

    如果要更改单元格颜色的样式,可以用下面的方法:

                ICellStyle style = workbook.CreateCellStyle();
                style.FillPattern = FillPattern.SolidForeground;//很重要
                style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
    
                IRow excelRow = workbook.GetSheetAt(0).GetRow(0);
                for (int i = 0; i <= 5; i++)
                {
                    excelRow.GetCell(i).CellStyle = style;
                }
                excelRow.GetCell(22).CellStyle = style;
                excelRow.GetCell(19).CellStyle = style;
                excelRow.GetCell(8).CellStyle = style;

    改颜色纠结了好久,也查了好多资料,网上有好多用cell.setcellstyle()这个方法的,我试了一下,根本没有,而且好多都是那样写的,不知道是java的还是啥的,应该不是.net的。

  • 相关阅读:
    ffmpeg 简单使用总结
    使用 Solr 构建企业级搜索服务器
    Linux 常用命令整理
    基于.net standard 的动态编译实现
    基于.net core 微服务的另类实现
    Java中线程总结
    关于EF中直接执行sql语句的参数化问题
    一个关于单据审核的流程演变
    java中匿名内部类总结
    Eclipse 中打开选中文件/文件夹所在目录
  • 原文地址:https://www.cnblogs.com/junshijie/p/6378200.html
Copyright © 2020-2023  润新知