NOPI 导出 Excel 2007_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > NOPI 导出 Excel 2007

NOPI 导出 Excel 2007

 2017/7/25 5:31:24  编编橙  程序员俱乐部  我要评论(0)
  • 摘要:代码:1publicstaticvoidThisTo<T>(List<T>source,string[]colums,Func<T,object[]>action,stringsavePath,stringsheetName="")2{3XSSFWorkbookxssfworkbook=newXSSFWorkbook();4ISheetsheet=xssfworkbook.CreateSheet(sheetName);5IRowrow=sheet
  • 标签:excel

代码:

class="code_img_closed" src="/Upload/Images/2017072505/0015B68B3C38AA5B.gif" alt="">
 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         }
logs_code_collapse">代码

 

调用:

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");
调用

 

发表评论
用户名: 匿名