Exemplo n.º 1
0
 /// <summary>
 /// EPPlus导出Excel(2003/2007)
 /// </summary>
 /// <typeparam name="T">泛型类型</typeparam>
 /// <param name="list">源泛型集合</param>
 /// <param name="fileName">文件名</param>
 /// <param name="columnName">表头数组</param>
 /// <param name="ext">扩展名(.xls|.xlsx)可选参数</param>
 /// <param name="responseEnd">是否输出结束,默认:是</param>
 /// <param name="action">sheet自定义处理委托</param>
 public static void EPPlusExportExcel <T>(List <T> list, string fileName, string[] columnName = null, string ext = ".xlsx", bool responseEnd = true, Action <ExcelWorksheet> action = null) where T : class, new()
 {
     if (list?.Count > 0)
     {
         try
         {
             using (var package = new ExcelPackage())
             {
                 //配置文件属性
                 package.Workbook.Properties.Category       = "类别";
                 package.Workbook.Properties.Author         = "作者";
                 package.Workbook.Properties.Comments       = "备注";
                 package.Workbook.Properties.Company        = "公司名称";
                 package.Workbook.Properties.Keywords       = "关键字";
                 package.Workbook.Properties.Manager        = "张强";
                 package.Workbook.Properties.Status         = "内容状态";
                 package.Workbook.Properties.Subject        = "主题";
                 package.Workbook.Properties.Title          = "标题";
                 package.Workbook.Properties.LastModifiedBy = "最后一次保存者";
                 using (var sheet = package.Workbook.Worksheets.Add("Sheet1"))
                 {
                     //设置Excel头部标题
                     if (columnName != null)
                     {
                         for (var i = 0; i < columnName.Length; i++)
                         {
                             sheet.Cells[1, i + 1].Value = columnName[i];
                         }
                         sheet.Cells["A2"].LoadFromCollection(list, false, TableStyles.Light10);
                     }
                     else
                     {
                         sheet.Cells["A1"].LoadFromCollection(list, true, TableStyles.Light10);
                     }
                     //单元格自动适应大小
                     sheet.Cells.AutoFitColumns();
                     //单独设置单元格
                     action?.Invoke(sheet);
                     //写到客户端(下载)
                     HttpContext.Current.Response.Clear();
                     HttpContext.Current.Response.Charset = "utf-8";
                     HttpContext.Current.Response.AddHeader("content-disposition", $"attachment;filename={HttpUtility.UrlEncode(fileName + ext, Encoding.UTF8)}");
                     HttpContext.Current.Response.ContentType     = "application/ms-excel";
                     HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
                     HttpContext.Current.Response.BinaryWrite(package.GetAsByteArray());
                     HttpContext.Current.Response.Flush();
                     if (responseEnd)
                     {
                         HttpContext.Current.Response.End();
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             LogHelper.Error(ex, "EPPlus导出Excel(2003/2007)");
         }
     }
     else
     {
         MessageHelper.Alert("暂无数据可导出!", HttpContext.Current.Request.Url.ToString(), 2);
     }
 }