asp.net webwofrm后台代碼----------建議Framework4.0及以上,3.5試過出現好多莫名錯誤...
首先導入兩個程序集。我的是 office2003,引用的COM里面的 Microsoft.office.Interop.Excel 14.0 和 程序集里的Microsoft Excel 12.0 Object Library。有多個版本,根據自己的office版本選擇。
添加之后引用里力會出現兩個DLL Microsoft.office.Core 和Microsoft.office.Interop.Excel
生成一下項目,如果出現 Excel.ApplicationClass()無法互嵌套操作類型 請改用適用的接口
把引用的Microsoft.Office.Interop.Excel 右擊屬性 嵌入互操作類型改為false即可
DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn(); dc1.ColumnName = "name"; dc1.DataType = typeof(string); DataColumn dc2 = new DataColumn(); dc2.ColumnName = "age"; dc2.DataType = typeof(int); dt.Columns.Add(dc1); dt.Columns.Add(dc2); for (int i = 0; i < 1000; i++) { DataRow dr = dt.NewRow(); dr[0] = "茂茂" + i + "號"; dr[1] = i; dt.Rows.Add(dr); } new Class1().DataTabletoExcel(dt, DateTime.Now.Ticks+".xlsx");
先造些數據,然后將datatable和要導出文件名傳遞給方法。
DataTabletoExcel 方法體,一般的DataTable可以直接導出。比較復雜的結構需要自己定義
/// ///不要圖省勁省略了System.Data。Microsoft.office.Interop.DataTable 和System.Data.DataTable 會產生歧義 public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName) { ///先得到datatable的行數 int rowNum = tmpDataTable.Rows.Count; ///列數 int columnNum = tmpDataTable.Columns.Count; ///聲明一個應用程序類實例 Application xlApp = new ApplicationClass(); //xlApp.DefaultFilePath = ""; ///默認文件路徑,將其設置路徑后發現沒什么變化。導出excel的路徑還是在參數strFileName里設置 //xlApp.DisplayAlerts = true; //xlApp.SheetsInNewWorkbook = 1;///返回或設置 Microsoft Excel 自動插入到新工作簿中的工作表數目。Long 類型,可讀寫。設置為2之后沒發現什么區別 //創建一個新工作簿 Workbook xlBook = xlApp.Workbooks.Add(); ///在工作簿中得到sheet。 _Worksheet oSheet = (_Worksheet)xlBook.Worksheets[1]; #region 繪制列 ///自定義方法,想sheet中繪制列 RangeBuild(oSheet, "A1", "A2", "編號"); RangeBuild(oSheet, "B1", "B2", "畜主"); RangeBuild(oSheet, "C1", "C2", "地址"); RangeBuild(oSheet, "D1", "D2", "區划"); RangeBuild(oSheet, "E1", "E2", "規模"); RangeBuild(oSheet, "F1", "H1", "總存欄量"); RangeBuild(oSheet, "F2", "F2", "期初"); RangeBuild(oSheet, "G2", "G2", "期末"); RangeBuild(oSheet, "H2", "H2", "變更"); RangeBuild(oSheet, "I1", "K1", "母豬"); RangeBuild(oSheet, "I2", "I2", "期初"); RangeBuild(oSheet, "J2", "J2", "期末"); RangeBuild(oSheet, "K2", "K2", "變更"); RangeBuild(oSheet, "L1", "N1", "肉豬"); RangeBuild(oSheet, "L2", "L2", "期初"); RangeBuild(oSheet, "M2", "M2", "期末"); RangeBuild(oSheet, "N2", "N2", "變更"); RangeBuild(oSheet, "O1", "Q1", "仔豬"); RangeBuild(oSheet, "O2", "O2", "期初"); RangeBuild(oSheet, "P2", "P2", "期末"); RangeBuild(oSheet, "Q2", "Q2", "變更"); RangeBuild(oSheet, "R1", "T1", "公豬"); RangeBuild(oSheet, "R2", "R2", "期初"); RangeBuild(oSheet, "S2", "S2", "期末"); RangeBuild(oSheet, "T2", "T2", "變更"); RangeBuild(oSheet, "U1", "W1", "總面積"); RangeBuild(oSheet, "U2", "U2", "期初"); RangeBuild(oSheet, "V2", "V2", "期末"); RangeBuild(oSheet, "W2", "W2", "變更"); RangeBuild(oSheet, "X1", "Z1", "批建"); RangeBuild(oSheet, "X2", "X2", "期初"); RangeBuild(oSheet, "Y2", "Y2", "期末"); RangeBuild(oSheet, "Z2", "Z2", "變更"); RangeBuild(oSheet, "AA1", "AC1", "未批建"); RangeBuild(oSheet, "AA2", "AA2", "期初"); RangeBuild(oSheet, "AB2", "AB2", "期末"); RangeBuild(oSheet, "AC2", "AC2", "變更"); #endregion //將DataTable中的數據導入Excel中 for (int i = 0; i < rowNum; i++) { for (int j = 0; j < columnNum; j++) { ///excel中的列是從1開始的 xlApp.Cells[i+2, j+1] = tmpDataTable.Rows[i][j].ToString(); } } ///保存,路徑一塊穿進去。否則回到一個很奇妙的地方,貌似是system32里 temp下.... oSheet.SaveAs(@"D:\a\" + strFileName); } private static void RangeBuild(_Worksheet oSheet,string startcell,string endcell,string value) { ///創建一個區域對象。第一個參數是開始格子號,第二個參數是終止格子號。比如選中A1——D3這個區域。 Range range = (Range)oSheet.get_Range(startcell, endcell); ///合並方法,0的時候直接合並為一個單元格 range.Merge(0); ///合並單元格之后,設置其中的文本 range.Value = value; //橫向居中 range.HorizontalAlignment = XlVAlign.xlVAlignCenter; ///字體大小 range.Font.Size = 18; ///字體 range.Font.Name = "黑體"; ///行高 range.RowHeight = 24; //自動調整列寬 range.EntireColumn.AutoFit(); //填充顏色 range.Interior.ColorIndex = 20; //設置單元格邊框的粗細 range.Cells.Borders.LineStyle = 1; }
我這樣導出的excel是這樣子的