• NPOI的使用


    1、下载/导包

      http://npoi.codeplex.com/releases

      下载:NPOI binary 2.1.3.1

      解压源码包后的文件目录结构

      ************************************

      +  dotnet2          

      +  dotnet4          

      +  logo            

      -  LICENSE

      -  Read Me.txt

      -  Release Notes.txt

      ************************************

      进入 dotnet2 || dotnet4  

    2、添加四个.dll引用

      NPOI.dll

      NPOI.OOXML.dll

      NPOI.OpenXml4Net.dll

      NPOI.OpenmlFormats.dll

    3、在官方文档拿示例

      https://dotblogs.com.tw/killysss/archive/2010/01/27/13344.aspx

    4、生成方法

    public ResultSet<T> ExportDataSetToExcel<T>(List<T> list, string downloadPath, string destFilename, string searchParams = "")
            {
                //创建excel 工作簿对象
                IWorkbook workbook = CreateWorkbook(downloadPath + destFilename);
    
                //创建excel 表对象
                ISheet sheet = CreateSheet<T>(list, searchParams, workbook);
    
                //写入到excel文件中
                var stream = WorkBookToMemoryStream(workbook);
                WriteSteamToFile(stream, downloadPath + destFilename);
    
                return new ResultSet<T> { IsSuccess = true };
            }
    
    private IWorkbook CreateWorkbook(string templateFileName)
            {
                using (FileStream file = new FileStream(templateFileName, FileMode.Open, FileAccess.Read))
                {
                    IWorkbook workbook;
                    if ((templateFileName.Substring(templateFileName.LastIndexOf(".")).IndexOf("xlsx")) > 0)
                    {
                        workbook = new XSSFWorkbook(file);
                    }
                    else
                    {
                        workbook = new HSSFWorkbook(file);
                    }
                    return workbook;
    
                }
    
            }
    
    private static ISheet CreateSheet<T>(List<T> list, string searchParams, IWorkbook workbook)
            {
                ISheet sheet = workbook.GetSheet("Sheet1");
    
                var bgRow = sheet.FirstRowNum;
                var edRow = sheet.LastRowNum;
    
                //插入查询条件到excel
                sheet.GetRow(2).GetCell(1).SetCellValue(searchParams);
    
                IRow rData = sheet.GetRow(bgRow);//excel表的第一行内容
    
                DataSet listDataSet = list.ToDataSet<T>();
                for (int i = 0; i < listDataSet.Tables[0].Rows.Count; i++)
                {
                    var row = listDataSet.Tables[0].Rows[i];
                    IRow irow = sheet.CreateRow(edRow + 1);
    
                    for (int j = rData.FirstCellNum; j < rData.LastCellNum; j++)
                    {
                        var columnDataSet = row[rData.GetCell(j).ToString()].IfNull(0);
                        var cell = irow.CreateCell(j);
                        cell.SetCellValue(columnDataSet.ToString());
                    }
                    edRow++;
                }
    
                //删除excel一第行
                sheet.ShiftRows(1, edRow, -1);
                return sheet;
            }
    

      

    /// <summary>
    /// 转换成输出流
    /// </summary>
    /// <param name="InputStream"></param>
    /// <returns></returns>
    public static MemoryStream WorkBookToMemoryStream(IWorkbook workbook)
    {
          MemoryStream file = new MemoryStream();
          workbook.Write(file);
          return file;
    }
    

     

    /// <summary>
    /// 写入到excel文件中
    /// </summary>
    /// <param name="ms"></param>
    /// <param name="FileName"></param>
    private static void WriteSteamToFile(MemoryStream ms, string FileName)
    {
          FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
          byte[] data = ms.ToArray();
    
          fs.Write(data, 0, data.Length);
          fs.Flush();
          fs.Close();
    
          data = null;
          ms = null;
          fs = null;
    }
    

      

    初步搞定,能根据传来的excel模板插入数据生成一个新的excel

    未来拿不出手,过去会有谁听。

    作者:aLong

    出处:http://www.cnblogs.com/keerdi/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Redis必须注意的慢查询问题
    使用kuernetes提供高可用的logstash服务
    使用kuernetes提供高可用的kibana服务
    Docker指定multiple Insecure registry的方法
    ELK系统中kibana展示数据的时区问题
    ElasticSearch快照备份及恢复
    离线安装redis集群
    Elasticsearch静态集群配置
    LCM的个数 (LCM Cardinality,UVa 10892)
    幂和阶乘 (Again Prime?No time.,UVa 10780)
  • 原文地址:https://www.cnblogs.com/tooy/p/7093188.html
Copyright © 2020-2023  润新知