作了好幾個Excel、Word導出,用了HTTP流導出僞Excel文件、用過Office組件(這東西在生產環境下至關麻煩,各類權限,**)。html
最後決定使用NPOI組件來導出,好處不少不少了,這裏很少說。數據庫
這篇文章呢,主要說一下Excel導出的細節以及問題。瀏覽器
我在製做這個Demo的時候使用的環境:ide
Visual Studio 20十、Office 2013 、Framework .NET 3.5 、NPOI 1.2.5(至於爲何沒有選最新版 稍後說)this
從瀏覽器導出的Excel,打開並無提示文件格式不對,這是真真的Excel格式。spa
從上圖看出,列是自動適應了寬度了的,不會擠到一堆。.net
/// <summary> /// 建立工做簿 /// </summary> /// <param name="fileName">下載文件名</param> /// <param name="dt">數據源</param> public static void CreateSheet(string fileName, DataTable dt) { HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); //建立一個名稱爲Payment的工做表 ISheet paymentSheet = workbook.CreateSheet("Payment"); //數據源 DataTable tbPayment = dt; //頭部標題 IRow paymentHeaderRow = paymentSheet.CreateRow(0); //循環添加標題 foreach (DataColumn column in tbPayment.Columns) paymentHeaderRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // 內容 int paymentRowIndex = 1; foreach (DataRow row in tbPayment.Rows) { IRow newRow = paymentSheet.CreateRow(paymentRowIndex); //循環添加列的對應內容 foreach (DataColumn column in tbPayment.Columns) { newRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } paymentRowIndex++; } //列寬自適應,只對英文和數字有效 for (int i = 0; i <= dt.Rows.Count; i++) { paymentSheet.AutoSizeColumn(i); } //獲取當前列的寬度,而後對比本列的長度,取最大值 for (int columnNum = 0; columnNum <= dt.Columns.Count; columnNum++) { int columnWidth = paymentSheet.GetColumnWidth(columnNum) / 256; for (int rowNum = 1; rowNum <= paymentSheet.LastRowNum; rowNum++) { IRow currentRow; //當前行未被使用過 if (paymentSheet.GetRow(rowNum) == null) { currentRow = paymentSheet.CreateRow(rowNum); } else { currentRow = paymentSheet.GetRow(rowNum); } if (currentRow.GetCell(columnNum) != null) { ICell currentCell = currentRow.GetCell(columnNum); int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; if (columnWidth < length) { columnWidth = length; } } } paymentSheet.SetColumnWidth(columnNum, columnWidth * 256); } //將表內容寫入流 通知瀏覽器下載 workbook.Write(ms); System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", fileName)); System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //進行二進制流下在 workbook = null; ms.Close(); ms.Dispose(); } /// <summary> /// 虛擬 DataTable內容 /// </summary> /// <returns></returns> public static DataTable CreatTable() { //建立DataTable 將數據庫中沒有的數據放到這個DT中 DataTable datatable = new DataTable(); datatable.Columns.Add("列1", typeof(string)); datatable.Columns.Add("列2", typeof(string)); datatable.Columns.Add("列3", typeof(string)); //建立DatatTable 結束--------------------------- //開始給臨時datatable賦值 for (int i = 0; i < 10; i++) { DataRow row = datatable.NewRow(); row["列1"] = "列111111111111111111111111111111"; row["列2"] = "列222222222222222222222222222222222222222"; row["列3"] = "列3333333322222222222211111111111111111111111113"; datatable.Rows.Add(row); } return datatable; }
代碼中我加上了一個本身建立的DataTable做爲數據源來進行導出,以避免Demo用到數據庫。code
當建立項目的版本高於NPOI基於的.net版本,會提示報錯。orm
更換.net版本就好了。htm
另外官網提供的最新版是bate版本,我使用過程當中會報錯。仍是選擇2.0版本吧。
360雲盤 http://yunpan.cn/Qagu3ZYyYYqnW (提取碼:0538)
此次直接來一個簡單的擴展,包含了泛型導出和DataTable導出:
1 /// <summary> 2 /// Export擴展 - DataTable、泛型導出Excel 3 /// </summary> 4 public static class ExportExcel 5 { 6 /// <summary> 7 /// 導出Excel(03-07) 泛型集合操做 8 /// </summary> 9 /// <typeparam name="T">實體類型</typeparam> 10 /// <param name="lists">數據源</param> 11 /// <param name="fileName">下載文件名</param> 12 /// <returns></returns> 13 public static byte[] ListToExcel<T>(this IList<T> lists, string fileName) 14 { 15 using (MemoryStream ms = new MemoryStream()) 16 { 17 HSSFWorkbook workbook = new HSSFWorkbook(); 18 //建立一個名稱爲Payment的工做表 19 ISheet paymentSheet = workbook.CreateSheet("Payment"); 20 //頭部標題 21 IRow paymentHeaderRow = paymentSheet.CreateRow(0); 22 23 PropertyInfo[] propertys = lists[0].GetType().GetProperties(); 24 //循環添加標題 25 for (int i = 0; i < propertys.Count(); i++) 26 paymentHeaderRow.CreateCell(i).SetCellValue(propertys[i].Name); 27 // 內容 28 int paymentRowIndex = 1; 29 foreach (var each in lists) 30 { 31 IRow newRow = paymentSheet.CreateRow(paymentRowIndex); 32 //循環添加列的對應內容 33 for (int i = 0; i < propertys.Count(); i++) 34 { 35 var obj = propertys[i].GetValue(each, null); 36 newRow.CreateCell(i).SetCellValue(obj.ToString()); 37 } 38 paymentRowIndex++; 39 } 40 41 //列寬自適應,只對英文和數字有效 42 for (int i = 0; i <= lists.Count; i++) 43 paymentSheet.AutoSizeColumn(i); 44 //將表內容寫入流 等待下一步操做 45 workbook.Write(ms); 46 return ms.ToArray(); 47 } 48 } 49 50 /// <summary> 51 /// 導出Excel(03-07) DataTable操做 52 /// </summary> 53 /// <param name="dt">數據源</param> 54 /// <param name="fileName">下載文件名</param> 55 /// <returns></returns> 56 public static byte[] ListToExcel(this DataTable dt, string fileName) 57 { 58 using (MemoryStream ms = new MemoryStream()) 59 { 60 HSSFWorkbook workbook = new HSSFWorkbook(); 61 //建立一個名稱爲Payment的工做表 62 ISheet paymentSheet = workbook.CreateSheet("Payment"); 63 //數據源 64 DataTable tbPayment = dt; 65 //頭部標題 66 IRow paymentHeaderRow = paymentSheet.CreateRow(0); 67 //循環添加標題 68 foreach (DataColumn column in tbPayment.Columns) 69 paymentHeaderRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); 70 // 內容 71 int paymentRowIndex = 1; 72 foreach (DataRow row in tbPayment.Rows) 73 { 74 IRow newRow = paymentSheet.CreateRow(paymentRowIndex); 75 //循環添加列的對應內容 76 foreach (DataColumn column in tbPayment.Columns) 77 newRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); 78 paymentRowIndex++; 79 } 80 81 //列寬自適應,只對英文和數字有效 82 for (int i = 0; i <= dt.Rows.Count; i++) 83 paymentSheet.AutoSizeColumn(i); 84 //獲取當前列的寬度,而後對比本列的長度,取最大值 85 for (int columnNum = 0; columnNum <= dt.Columns.Count; columnNum++) 86 { 87 int columnWidth = paymentSheet.GetColumnWidth(columnNum) / 256; 88 for (int rowNum = 1; rowNum <= paymentSheet.LastRowNum; rowNum++) 89 { 90 //當前行未被使用過 91 var currentRow = paymentSheet.GetRow(rowNum) ?? paymentSheet.CreateRow(rowNum); 92 if (currentRow.GetCell(columnNum) != null) 93 { 94 ICell currentCell = currentRow.GetCell(columnNum); 95 int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; 96 if (columnWidth < length) 97 columnWidth = length; 98 } 99 } 100 paymentSheet.SetColumnWidth(columnNum, columnWidth * 256); 101 } 102 //將表內容寫入流 等待其餘操做 103 workbook.Write(ms); 104 return ms.ToArray(); 105 } 106 } 107 }
解決自適應寬不支持中文問題: http://blog.csdn.net/jerry_cool/article/details/7000085
NPOI官網:http://npoi.codeplex.com/