资源描述:
《将数据库表导出到excel》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、将数据库表导出到Excel,并生成文件(C#实现)关键词:C#, ASP.NET, Excel 需添加项目引用:1..NET->System.Data.OracleClient.dll2.COM->MicrosoftExcel11.0ObjectLibrary代码如下:usingSystem;usingSystem.IO;usingSystem.Data;usingSystem.Reflection;usingSystem.Diagnostics;usingSy
2、stem.Configuration;usingSystem.Collections;usingExcel;namespaceProtoType{ /// ///套用模板输出Excel,生成xls文件和html文件 ///Author:LiuWen ///DateCreated:2006-8 /// publicclassExportExcel { #regionvariablemember protectedstringtemplateFile=null; protectedstringexcelFile=null; protect
3、edstringhtmlFile=null; protectedobjectmissing=Missing.Value; Excel.ApplicationClassapp; Excel.Workbookbook; Excel.Worksheetsheet; Excel.Rangerange; privateDateTimebeforeTime; //Excel启动之前时间 privateDateTimeafterTime; //Excel启动之后时间 #endregion /// ///构造函数,将一个已有Excel
4、工作簿作为模板,并指定输出路径 /// ///Excel模板文件路径 ///Excel输出文件路径 ///Html输出文件路径 publicExportExcel(stringtemplateFile,stringexcelFile,stringhtmlFile) { if(templateFile==null) thrownewException("Excel模板文件路径不能为空!"); if(excelFile==null) thrownewException("Excel输出文件路径不能为空!");
5、if(htmlFile==null) thrownewException("Html输出文件路径不能为空!"); if(!File.Exists(templateFile)) thrownewException("指定路径的Excel模板文件不存在!"); this.templateFile=templateFile; this.excelFile=excelFile; this.htmlFile=htmlFile; //创建一个Application对象 beforeTime=DateTime.Now;
6、 app=newApplicationClass(); //app.Visible=true; afterTime=DateTime.Now; //打开模板文件,得到WorkBook对象 try { book=app.Workbooks.Open(templateFile,missing,missing,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing,missing); }
7、 catch(Exceptione) { throwe; } //得到WorkSheet对象 sheet=(Excel.Worksheet)book.Sheets.get_Item(1); } /// ///将DataTable数据导出到Excel(可动态插入行) /// ///DataTable ///插入行的索引 ///插入列的索引 publicvoidDataTableToExcel(System.Data.DataTabledt,introwIndex,intcolIndex) { //r
8、ange=sheet.get_Range("A7",missing); //range.Value2="raogerrr"; //stringstr=ran