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是这样子的