在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
将(Oracle)数据库表导出到Excel,并生成文件(C#实现)关键词: C#, ASP.NET, Excel
需添加项目引用: 1. .NET->System.Data.OracleClient.dll 2. COM->Microsoft Excel 11.0 Object Library 代码如下: using System; namespace thscjy /// 套用模板输出Excel,生成xls文件和html文件 /// Author: Liu Wen /// Date Created: 2006-8 /// public class ExportExcel { #region variable member 成员变量 protected string templateFile = null; protected string excelFile = null; protected string htmlFile = null; protected object missing = Missing.Value; Excel.ApplicationClass app; Excel.Workbook book; Excel.Worksheet sheet; Excel.Range range; private DateTime beforeTime; //Excel启动之前时间 private DateTime afterTime; //Excel启动之后时间 //private int processID; #endregion /// /// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径 /// /// Excel模板文件路径 /// Excel输出文件路径 /// Html输出文件路径 public ExportExcel(string templateFile, string excelFile, string htmlFile) { if (templateFile == null) throw new Exception("Excel模板文件路径不能为空!"); if (excelFile == null) if (htmlFile == null) if (!File.Exists(templateFile)) this.templateFile = templateFile; //创建一个Application对象 //打开 #region 插入报表参数 /// 录入报表的参数(TJ统计用) /// /// 填报单位 /// 年月 /// 填报人 /// 填报日期 /// 插入数据的单元格 public void InsertArgs(string department, string date, string accountant, string dateCreated, string cellID) { sheet.get_Range("A3", missing).Value2 = "填报单位:"+department; sheet.get_Range("D3", missing).Value2 = date; sheet.get_Range(cellID, missing).Value2 = "部门负责人: 填报人:"+accountant+" 联系电话: 报送时间:"+dateCreated; //sheet.get_Range("I8", missing).Value2 = "填报日期:"+dateCreated; } /// /// 录入报表的参数(JH计划用) /// /// 标题 public void InsertArgsJH(string name) { sheet.get_Range("A1", missing).Value2 = name; } /// /// 录入报表的参数(JH计划用) /// /// 标题 /// 年份 public void InsertArgsJH(string name, string year) { sheet.get_Range("A1", missing).Value2 = name; sheet.get_Range("D2", missing).Value2 = year; } /// /// 录入报表的参数(JH计划用) /// /// 标题 /// 部门 /// “部门”单元格ID /// 年份 /// “年份”单元格ID public void InsertArgsJH(string name, string department, string depCellId, string year, string yearCellId) { sheet.get_Range("A1", missing).Value2 = name; sheet.get_Range(depCellId, missing).Value2 = department; sheet.get_Range(yearCellId, missing).Value2 = year; } /// /// 录入 /// /// 标题 /// 注水 /// 注气 /// 措施工作量 public void InsertArgsJH(string name, string water, string gas, string workload) { sheet.get_Range("A1", missing).Value2 = name; sheet.get_Range("C2", missing).Value2 = water; sheet.get_Range("E2", missing).Value2 = gas; sheet.get_Range("G2", missing).Value2 = workload; } #endregion #region 导出Excel方法 /// /// 将DataTable数据导出到Excel(可动态插入行) /// /// DataTable /// 插入行的索引 /// 插入列的索引 public void DataTableToExcel(System.Data.DataTable dt, int rowIndex, int colIndex) { int rowCount = dt.Rows.Count; //DataTable行数 int colCount = dt.Columns.Count; //DataTable列数 int iRow; int iCol; //将数据导出到相应的单元格 //this.OutputFile(); //Excel.QueryTables qts = sheet.QueryTables; /// /// 将DataTable数据导出到Excel(可动态插入行) /// /// DataTable /// 插入数据的起始单元格 public void DataTableToExcel(System.Data.DataTable dt, string cellID) { int rowIndex = sheet.get_Range(cellID, missing).Row; int colIndex = sheet.get_Range(cellID, missing).Column; int rowCount = dt.Rows.Count; //DataTable行数 int colCount = dt.Columns.Count; //DataTable列数 int iRow; int iCol; //利用二维数组批量写入 for (iRow = 0; iRow < rowCount; iRow++) range = sheet.get_Range(cellID, missing); /// /// 将DataTable数据导出到Excel(固定) /// /// DataTable /// 插入数据的起始单元格 public void DataTableToExcel2(System.Data.DataTable dt, string cellID) { int rowCount = dt.Rows.Count; //DataTable行数 int colCount = dt.Columns.Count; //DataTable列数 int iRow; int iCol; //利用二维数组批量写入 range = sheet.get_Range(cellID, missing); /// /// 最后调用,释放相关资源,完成 /// public void Finalize() { this.OutputFile(); GC.Collect(); //this.KillExcelProcess(); } /// /// 输出生成的Excel, Html /// private void OutputFile() { //如果文件已存在,删除,重新生成 if (File.Exists(excelFile)) { File.Delete(excelFile); } if (File.Exists(htmlFile)) { File.Delete(htmlFile); } try { book.SaveAs(excelFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing,missing); book.SaveAs(htmlFile, Excel.XlFileFormat.xlHtml, missing, missing, missing, missing, /// /// 在工作表中插入行,并调整其他行以留出空间 /// /// 当前工作表 /// 欲插入的行索引 private void InsertRows(Excel.Worksheet sheet, int rowIndex) { range = (Excel.Range)sheet.Rows[rowIndex, missing]; //object Range.Insert(object shift, object copyorigin); //shift: Variant类型,可选。指定 //xlShiftToRight 或 xlShiftDown。如果省略该参数,Microsoft Excel 将根据区域形状确定调整方式。 range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, missing); } /// /// 在工作表中删除行 /// /// 当前工作表 /// 欲删除的行索引 private void DeleteRows(Excel.Worksheet sheet, int rowIndex) { range = (Range)sheet.Rows[rowIndex, missing]; range.Delete(Excel.XlDeleteShiftDirection.xlShiftUp); } /// /// 退出Excel,并且释放调用的COM资源 /// private void Dispose() { book.Close(missing, missing, missing); app.Workbooks.Close(); app.Quit(); if (range != null) //System.GC.Collect(); /// /// 结束Excel进程 /// private void KillExcelProcess() { DateTime startTime; Process[] processes = Process.GetProcessesByName("Excel"); //得不到Excel进程ID,暂时只能判断进程启动时间 } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论