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的。

  • 相关阅读:
    CentOS 6.5下Git服务器搭建
    汉化Eclipse+配色方法(官方语言包)
    Cocos2d-x内存管理研究<二>
    PHP模板解析类实例
    smarty的ASSIGN()函数
    详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
    phpstorm 格式化代码方法
    php get_magic_quotes_gpc()函数用法介绍
    php示例代码
    MySQL数据类型:SQL_MODE设置不容忽视
  • 原文地址:https://www.cnblogs.com/junshijie/p/6378200.html
Copyright © 2011-2022 走看看