zoukankan      html  css  js  c++  java
  • NOPI 导出 Excel 2007

    代码:

     1    public static void ThisTo<T>( List<T> source, string[] colums, Func<T, object[]> action, string savePath, string sheetName = "") 
     2         {
     3             XSSFWorkbook xssfworkbook = new XSSFWorkbook();
     4             ISheet sheet = xssfworkbook.CreateSheet(sheetName);
     5             IRow row = sheet.CreateRow(0);
     6             for (int i = 0; i < colums.Length; i++)
     7             {
     8                 ICell cell = row.CreateCell(i);
     9                 cell.SetCellValue(colums[i]);
    10             }
    11             for (int i = 0; i < source.Count; i++)
    12             {
    13                     IRow row1 = sheet.CreateRow(i + 1);
    14                      int colIndex = 0;
    15                      row1.Cells.AddRange(action(source[i]).Select(p =>
    16                      {
    17                         var colCell = row1.CreateCell(colIndex,CellType.String);
    18                         colCell.SetCellValue(p.ToString());
    19                         colIndex++;
    20                         return colCell;
    21                     }));
    22             }
    23             MemoryStream stream = new MemoryStream();
    24             xssfworkbook.Write(stream);
    25             var buf = stream.ToArray();
    26             using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
    27             {
    28                 fs.Write(buf, 0, buf.Length);
    29                 fs.Flush();
    30             }
    31         }

    调用:

    1  ThisTo<NAMES>(a, new string[] { "Name","Gender"}, p =>
    2             {
    3                 return new object[] {
    4                   p.Name,
    5                   p.Gender
    6                 };
    7             }, @"D:ABCD.xlsx", "234");

    说明:

           方法中第一个参数为需导出数据,第二个参数为Excel列名,第三个为导出时数据排布顺序(与第二个参数相对应),第四参数为保存地址,第五参数为sheet名,

          调用参照上述调用方法。

  • 相关阅读:
    mysql week 的使用方法
    获取某个周在本年的开始日期和结束日期
    question and answer
    系统安装
    adblock 下载地址
    windows新增/修改/删除系统环境变量bat示例,一键配置JAVA_HOME
    SpringMVC 复杂对象数据绑定
    IntelliJ IDEA default settings 全局默认设置
    Spring整合Ehcache管理缓存
    label的for属性
  • 原文地址:https://www.cnblogs.com/lvlinlv/p/7228118.html
Copyright © 2011-2022 走看看