在很多系统中都用到导出,使用过多种导出方式,觉得ClosedXML插件的导出简单又方便。
并且ClosedXML、DocumentFormat.OpenXml都是MIT开源。
首先通过 Nuget 安装 ClosedXML 插件,同时会自动安装 DocumentFormat.OpenXml 插件。
以下就是导出相关的代码:
1、MVC控制器中的导出
// MVC 控制器中 /// <summary> /// 导出 (无论是否需要返回值,都有 Response) /// </summary> public void Export1() // 传入搜索条件 { // 1、根据条件查询到想要的数据 DataTable data = new DataTable(); // 2、设置表名(对应sheet名)、列名(对应列名) data.TableName = "XX统计"; // 3、列宽设置(需要慢慢设置) int[] colsWidth = new int[] { 160, 200, 300, 160 }; // 4、生成导出文件 byte[] filedata = ExportHelper.ExportExcel(data); // 5、输出文件流 HttpContext.Output(filedata, "XX统计_导出" + DateTime.Today.ShowDate() + ".xlsx"); }
2、生成导出文件
using ClosedXML.Excel; using System.Data; using System.IO; /// <summary> /// 导出Excel文件 /// </summary> /// <param name="data">导出的数据</param> /// <param name="colsWidth">列宽</param> /// <returns></returns> public static byte[] ExportExcel(DataTable data, int[] colsWidth = null) { using (XLWorkbook workbook = new XLWorkbook()) { IXLWorksheet worksheet = workbook.AddWorksheet(data.TableName); // 处理列 for (int i = 0; i < data.Columns.Count; i++) { worksheet.Cell(1, i + 1).Value = data.Columns[i].ColumnName; worksheet.Cell(1, i + 1).Style.Font.Bold = true; } // 处理列宽 if (colsWidth != null) { for (int j = 1; j <= colsWidth.Length; j++) { worksheet.Columns(j, j).Width = colsWidth[j - 1]; } } // 处理数据 int r = 2;// 第二行开始 foreach (DataRow dr in data.Rows) { // 第一列开始 for (int c = 1; c <= data.Columns.Count; c++) {
worksheet.Cell(r, c).SetValue<string>(dr[c-1].ToString()); // worksheet.Cell(r, c).Value = dr[c-1].ToString(); // 存在数据类型隐患,自动转换数字、日期、时间格式的数据,显示出来的可能不是想要的
// worksheet.Cell(r, c).DataType = XLDataType.Text; // 1、先设置格式,赋值后,会自动根据数据重新改变格式,2、先赋值,后设置格式,存在日期变为数字情况; } r++; } // 缓存到内存流,然后返回 using (MemoryStream stream = new MemoryStream()) { workbook.SaveAs(stream); return stream.ToArray(); } } }
3、输出文件流
using System.Text; using System.Text.RegularExpressions; using System.Web; /// <summary> /// 输出文件流 /// </summary> /// <param name="httpContext">Http上下文</param> /// <param name="filedata">文件数据</param> /// <param name="fileName">文件名(要后缀名)</param> public static void Output(this HttpContextBase httpContext, byte[] filedata, string fileName) { //文件名称效验 文件名不能包含/:*?<>| 其中需要两次转义 if (Regex.IsMatch(fileName, @"[\/:*?<>|]")) { fileName = Regex.Replace(fileName, @"[\/:*?<>|]", "_"); } //判断是否为火狐浏览器(下载时,有些浏览器中文名称乱码,有些浏览器中文名正常,但不能编码,不会自动解码,如火狐) if (httpContext.Request.Browser.Browser != "Firefox") { fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8); } // 没有定义类型,用二进制流类型的MIME string MIME = "application/octet-stream"; httpContext.Response.Clear(); httpContext.Response.Buffer = true; //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送 httpContext.Response.ContentType = MIME; httpContext.Response.ContentEncoding = Encoding.UTF8; httpContext.Response.Charset = Encoding.UTF8.HeaderName; httpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); httpContext.Response.AddHeader("Accept-Language", "zh-cn"); httpContext.Response.AddHeader("Content-Length", filedata.LongLength.ToString()); httpContext.Response.BinaryWrite(filedata); httpContext.Response.Flush(); httpContext.Response.End(); }
通过ClosedXML导出操作方便。