在很多系統中都用到匯出,使用過多種匯出方式,覺得ClosedXML插件的匯出簡單又方便,
并且ClosedXML、DocumentFormat.OpenXml都是MIT開源,
首先通過 Nuget 安裝 ClosedXML 插件,同時會自動安裝 DocumentFormat.OpenXml 插件,
以下就是匯出相關的代碼:
1、MVC控制器中的匯出
// MVC 控制器中 /// <summary> /// 匯出 (無論是否需要回傳值,都有 Response) /// </summary> public void Export1() // 傳入搜索條件 { // 1、根據條件查詢到想要的資料 DataTable data = https://www.cnblogs.com/miaolin/p/new DataTable(); // 2、設定表名(對應sheet名)、列名(對應列名) data.TableName = "XX統計"; // 3、列寬設定(需要慢慢設定) int[] colsWidth = new int[] { 160, 200, 300, 160 }; // 4、生成匯出檔案 byte[] filedata =https://www.cnblogs.com/miaolin/p/ 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 =https://www.cnblogs.com/miaolin/p/ 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 = https://www.cnblogs.com/miaolin/p/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匯出操作方便,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/86464.html
標籤:C#
