zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    webug第四关:告诉你了flang是5位数
    webug第三关:你看到了什么?
    webug第二关:从图片中你能找到什么?
    webug第一关:很简单的一个注入
    redhat-DHCP服务的配置与应用
    python-基础入门-7基础
    bWAPP----SQL Injection (GET/Search)
    ctf-web-sql
    光棍节程序员闯关秀writeup
    ajax回调函数Status问题
  • 原文地址:https://www.cnblogs.com/zengdingding/p/6207352.html
Copyright © 2011-2022 走看看