资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
将数据库表导出到将数据库表导出到 Excel,并生成文件(,并生成文件(C#实现)实现)关键词关键词: C#, ASP.NET, Excel 需添加项目引用:1. .NET-System.Data.OracleClient.dll2. COM-Microsoft Excel 11.0 Object Library代码如下:using System;using System.IO;using System.Data;using System.Reflection;using System.Diagnostics;using System.Configuration;using System.Collections;using Excel;namespace ProtoType/ / 套用模板输出 Excel,生成 xls 文件和 html 文件/ Author: Liu Wen/ Date Created: 2006-8/ public class ExportExcel#region variable memberprotected 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 启动之后时间#endregion/ / 构造函数,将一个已有 Excel 工作簿作为模板,并指定输出路径/ / Excel 模板文件路径 / Excel 输出文件路径 / Html 输出文件路径 public ExportExcel(string templateFile, string excelFile, string htmlFile)if(templateFile = null)throw new Exception(“Excel 模板文件路径不能为空!“);if(excelFile = null)throw new Exception(“Excel 输出文件路径不能为空!“);if(htmlFile = null)throw new Exception(“Html 输出文件路径不能为空!“);if(!File.Exists(templateFile)throw new Exception(“指定路径的 Excel 模板文件不存在!“);this.templateFile = templateFile;this.excelFile = excelFile;this.htmlFile = htmlFile;/创建一个 Application 对象beforeTime = DateTime.Now;app = new ApplicationClass();/app.Visible = true;afterTime = DateTime.Now;/打开模板文件,得到 WorkBook 对象trybook = app.Workbooks.Open(templateFile,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);catch (Exception e)throw e;/得到 WorkSheet 对象sheet = (Excel.Worksheet)book.Sheets.get_Item(1);/ / 将 DataTable 数据导出到 Excel(可动态插入行)/ / DataTable / 插入行的索引 / 插入列的索引 public void DataTableToExcel(System.Data.DataTable dt, int rowIndex, int colIndex)/range = sheet.get_Range(“A7“, missing);/range.Value2 = “raogerrr“;/string str = range.Text.ToString();int rowCount = dt.Rows.Count; /DataTable 行数int colCount = dt.Columns.Count; /DataTable 列数int iRow;int iCol;/将数据导出到相应的单元格for (iRow = 0; iRow beforeTime
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号