NOPI导出加载模板

Posted MrNosin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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导出加载模板的主要内容,如果未能解决你的问题,请参考以下文章

NOPI导入导出

NOPI实现导入导出泛型List,支持自定义列

NOPI导出多个sheet,一列图片

c#使用NPOI进行Excel导入导出,附源码,vs2010

微信小程序开发--模板(template)使用,数据加载,点击交互

C#怎么讲DataTable导出到Excel