DataTable数据导入Excel(导入到三个工作表)
2011-08-24 09:55
453 查看
Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); #region 新建一个Excel if (sheetName == "PaymentDetail")//第二次进入时不需要创建Excel { xlApp.DefaultFilePath = ConfigurationSettings.AppSettings["ExcelFilesDriver"].ToString(); xlApp.DisplayAlerts = true; xlApp.SheetsInNewWorkbook = 1; Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); xlBook.SaveCopyAs("PaymentExport-" + Yok.Components.YokContext.Current.CurrentUser.CompanyID.ToString() + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); } #endregion string filePath = xlApp.DefaultFilePath + "PaymentExport-" + Yok.Components.YokContext.Current.CurrentUser.CompanyID.ToString() + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"; object oMissing = System.Reflection.Missing.Value; xlApp.Visible = false; xlApp.DisplayAlerts = false; Microsoft.Office.Interop.Excel.Workbooks oBooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel._Workbook xlWorkbook = null; xlWorkbook = oBooks.Open(filePath,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing, oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing); Microsoft.Office.Interop.Excel.Worksheet xlWorksheet; // 添加入一个新的Sheet页。 xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing); //新加的Sheet页名。 xlWorksheet.Name = sheetName; #region 导入数据到Excel //将DataTable的列名导入Excel表第一行 int rowNum = dt.Rows.Count; int columnNum = dt.Columns.Count; int rowIndex = 1; int columnIndex = 0; foreach (DataColumn dc in dt.Columns) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName; } //将DataTable中的数据导入Excel中 for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = dt.Rows[i][j].ToString(); } } #endregion // 保存并关闭这个工作簿。 xlWorkbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null; //结束进程,释放资源 xlApp.Quit(); GC.Collect(); #region 输出IO并提供下载 if (sheetName == "PaymentApprovalDetail")//只有运行第二遍时才打印输出 { this.btnDeleteTable.Visible = true; FileInfo Fi = new FileInfo(filePath); if (Fi.Exists) { FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.Charset = "GBK"; Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=PaymentExport"+ DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } #endregion
相关文章推荐
- 怎样将Datatable中的数据直接导入到Excel模板上
- DataTable数据导入到Excel
- DataTable数据导入到Excel 引用Microsoft Office 12.0 Object Library和 Microsoft Excel 14.0 Object Library 并且需要注意的问题
- 把Excel文件数据导入数据库,支持多工作表
- 把DataSet或者DataTable里的数据导入Excel中
- asp.net 将Excel中某个工作簿的数据导入到DataTable方法
- c# 导入excel 读取excel数据 多个工作表时 解决科学计数法显示的问题
- 将excel中的数据导入到DataTable中
- qtp:将excel数据导入DataTable并从第一行数据开始取值
- 如何利用脚本把excel数据导入DataTable
- DataTable数据导入Excel
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
- 使用NPOI将DataTable中的数据导入到Excel中
- 让多个DataTable中的数据导入到一个excel中sheet中
- 把数据从DataTable导入到Excel文件里并创建表
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
- asp.net(C#)开发之一 dataset(datatable) 数据导入excel
- 把Excel文件数据导入数据库,支持多工作表
- [导入]asp.net 把datatable的数据导出到EXCEL
- 使用OpenXml操作Excel,创建excel,导入DataTable的数据等