将数据库表导出到Excel

上传人:飞*** 文档编号:40542715 上传时间:2018-05-26 格式:DOC 页数:7 大小:43KB
返回 下载 相关 举报
将数据库表导出到Excel_第1页
第1页 / 共7页
将数据库表导出到Excel_第2页
第2页 / 共7页
将数据库表导出到Excel_第3页
第3页 / 共7页
将数据库表导出到Excel_第4页
第4页 / 共7页
将数据库表导出到Excel_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《将数据库表导出到Excel》由会员分享,可在线阅读,更多相关《将数据库表导出到Excel(7页珍藏版)》请在金锄头文库上搜索。

1、将数据库表导出到将数据库表导出到 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.Co

2、llections;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 = Missi

3、ng.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 tem

4、plateFile, 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 模板文件不存在!“

5、);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,missi

6、ng,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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号