zoukankan      html  css  js  c++  java
  • 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的。

  • 相关阅读:
    HDU 1754线段树基本操作,建树,更新,查询
    用第三方下载工具下载官方XCode独立安装包的方法
    解决Windows平台通过cURL上传APP到蒲公英pgyer平台时无法使用中文升级描述的问题
    Cygwin 版本的 Curl 安装,提取,使用笔记
    Android Gradle 引用本地 AAR 的几种方式
    VM虚拟机快照还原效果实现方式
    插入中文错误ERROR 1406 (22001): Data too long for column 'name' at row 1
    mysql初始化默认为空的密码修改
    性能测试学习计划(转)
    context-param和init-param区别
  • 原文地址:https://www.cnblogs.com/junshijie/p/6378200.html
Copyright © 2011-2022 走看看