方法1:微软推荐服务器需安装Excel型
依赖:
软件:Office Excel 2007-2013
引用:Microsoft Office 14.0 Object Library
1-1.数据准备
1 //Excel文件名称 2 string ExcelName = System.DateTime.Now.ToString("yyMMdd") + "退款结算单.xlsx"; 3 4 /// <summary> 5 /// 批量退款 6 /// </summary> 7 /// <param name="sender"></param> 8 /// <param name="e"></param> 9 protected void Button1_Click(object sender, EventArgs e) 10 { 11 DataSet ds = new DataSet(); 12 13 DataTable dt1 = new DataTable(); 14 dt1.Columns.Add("批次号"); 15 dt1.Columns.Add("总金额(元)"); 16 dt1.Columns.Add("总笔数"); 17 18 DataRow row1 = dt1.NewRow(); 19 20 row1 = dt1.NewRow(); 21 row1["批次号"] = "20150425001"; 22 row1["总金额(元)"] = "0.03"; 23 row1["总笔数"] = "1"; 24 dt1.Rows.Add(row1); 25 26 ds.Tables.Add(dt1); 27 28 DataTable dt2 = new DataTable(); 29 dt2.Columns.Add("商户订单号"); 30 dt2.Columns.Add("支付宝交易号"); 31 dt2.Columns.Add("退款金额"); 32 dt2.Columns.Add("退款备注"); 33 34 DataRow row2 = dt2.NewRow(); 35 36 row2 = dt2.NewRow(); 37 row2["商户订单号"] = "D150418092109-40"; 38 row2["支付宝交易号"] = "2015041800001000770049196543"; 39 row2["退款金额"] = "0.02"; 40 row2["退款备注"] = "导入excel测试"; 41 dt2.Rows.Add(row2); 42 43 ds.Tables.Add(dt2); 44 45 46 doExport(ds, Server.MapPath(ExcelName)); 47 }
1-2.数据写入
1 private Microsoft.Office.Interop.Excel.Application _Excel = null; 2 3 /// <summary> 4 /// 将 DataSet 数据写入 _Excel 5 /// </summary> 6 /// <param name="ds"></param> 7 /// <param name="strExcelFileName"></param> 8 private void doExport(DataSet ds, string strExcelFileName) 9 { 10 int rowIndex = 1;//当前行数 11 int colIndex = 0;//当前列数 12 13 14 _Excel = new Microsoft.Office.Interop.Excel.Application(); 15 _Excel.Application.Workbooks.Add(true); 16 17 18 for (int i = 0; i < ds.Tables.Count; i++) 19 { 20 DataTable table = ds.Tables[i]; 21 colIndex = 0;//列初始化 22 // 列标题 23 foreach (DataColumn col in table.Columns) 24 { 25 colIndex++; 26 _Excel.Cells[rowIndex, colIndex] = col.ColumnName; 27 } 28 29 // 内容 30 foreach (DataRow row in table.Rows) 31 { 32 rowIndex++; 33 colIndex = 0; 34 foreach (DataColumn col in table.Columns) 35 { 36 colIndex++; 37 _Excel.Cells[rowIndex, colIndex] = "‘" + row[col.ColumnName].ToString(); 38 } 39 } 40 41 rowIndex++;//行+1 防止table又多个情况 42 } 43 44 _Excel.Visible = false; 45 // 保存文件 46 SaveToDisk(strExcelFileName); 47 48 _Excel.Quit(); 49 _Excel = null; 50 }
1-3.数据导出
1 /// <summary> 2 /// 导出方法 3 /// </summary> 4 /// <param name="path">路径</param> 5 private void SaveToDisk(string path) 6 { 7 if (!string.IsNullOrEmpty(path)) 8 { 9 System.IO.FileInfo info = new FileInfo(path); 10 if (!info.Exists) 11 { 12 _Excel.ActiveWorkbook.SaveCopyAs(path); 13 14 /* 15 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 16 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 17 代码如下: 18 */ 19 Response.ContentType = "text/xml"; 20 Response.AddHeader("Content-Disposition", "attachment;filename=" + ExcelName); 21 string filename = Server.MapPath(ExcelName); 22 Response.TransmitFile(filename); 23 } 24 else 25 { 26 info.Delete(); 27 _Excel.DisplayAlerts = false; 28 _Excel.AlertBeforeOverwriting = false; 29 _Excel.ActiveWorkbook.SaveCopyAs(path); 30 } 31 } 32 }
时间: 2024-11-05 13:03:52