首页 > 代码库 > NOPI导出加载模板

NOPI导出加载模板

ListExcel导出(加载模板)

 1 /// <summary>
 2         /// List根据模板导出ExcelMemoryStream
 3         /// </summary>
 4         /// <param name="list"></param>
 5         /// <param name="templdateName"></param>
 6         public static MemoryStream ExportListByTempale(List<TemplateMode> list, string templdateName)
 7         {
 8             try
 9             {
10 
11                 string templatePath = HttpContext.Current.Server.MapPath("/") + "/Resource/ExcelTemplate/";
12                 string templdateName1 = string.Format("{0}{1}", templatePath, templdateName);
13 
14                 FileStream fileStream = new FileStream(templdateName1, FileMode.Open, FileAccess.Read);
15                 ISheet sheet = null;
16                 if (templdateName.IndexOf(".xlsx") == -1)//2003
17                 {
18                     HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileStream);
19                     sheet = hssfworkbook.GetSheetAt(0);
20                     SetPurchaseOrder(sheet, list);
21                     sheet.ForceFormulaRecalculation = true;
22                     using (MemoryStream ms = new MemoryStream())
23                     {
24                         hssfworkbook.Write(ms);
25                         ms.Flush();
26                         return ms;
27                     }
28                 }
29                 else//2007
30                 {
31                     XSSFWorkbook xssfworkbook = new XSSFWorkbook(fileStream);
32                     sheet = xssfworkbook.GetSheetAt(0);
33                     SetPurchaseOrder(sheet, list);
34                     sheet.ForceFormulaRecalculation = true;
35                     using (MemoryStream ms = new MemoryStream())
36                     {
37                         xssfworkbook.Write(ms);
38                         ms.Flush();
39                         return ms;
40                     }
41                 }
42 
43             }
44             catch (Exception)
45             {
46                 throw;
47             }
48         }
49         /// <summary>
50         /// 赋值单元格
51         /// </summary>
52         /// <param name="sheet"></param>
53         /// <param name="list"></param>
54         private static void SetPurchaseOrder(ISheet sheet, List<TemplateMode> list)
55         {
56             try
57             {
58                 foreach (var item in list)
59                 {
60                     IRow row = null;
61                     ICell cell = null;
62                     row = sheet.GetRow(item.row);
63                     if (row == null)
64                     {
65                         row = sheet.CreateRow(item.row);
66                     }
67                     cell = row.GetCell(item.cell);
68                     if (cell == null)
69                     {
70                         cell = row.CreateCell(item.cell);
71                     }
72                     cell.SetCellValue(item.value);
73                 }
74             }
75             catch (Exception)
76             {
77                 throw;
78             }
79         }
80         #endregion

 

NOPI导出加载模板