.net里导出excel表方法汇总.doc

.net里导出excel表方法汇总.doc

CSDN - 文档中心 - .NET 阅读:7199 ??评论: 5 ?? 参与评论 标题 ? 从DataView中生成Excel报表的方案(C#)???? 选择自 rexsp 的 Blog 关键字 ? Excel DataView 出处 ? 前言: 前几天一同事问我如何利用C#将数据导到Excel文件当中,当时比较忙没有 顾得上去研究,今天特地研究了一下,基本搞定,下面就具体介绍如何将 DataView中的数据按照一定格式存到Excel文件当中。 正文: 一、首先要引用一个Excel的组件,我一开始是在Office XP下尝试的,不 成功,后来把XP给干掉,装2k,就成功了,所以这里分享的是Office 2k下 引用相关组件来实现功能的,在工程中引用COM标签中的Microsoft Excel 9.0 Object Library,添加成功后,引用中会多出三个引用项: Excel、Office、VBIDE。 ? ? ? 二、具体代码。 using System; using System.Data; using Excel; using System.IO; namespace Test.ExcelCom { /// summary /// 将DataView中的数据导入Excel文件中 /// 作者:Rexsp /// 创建:2004-4-4 /// /summary public class OutputExcel { ?#region 私有成员 ?/// summary ?/// 数据的DataView ?/// /summary ?private DataView dv=null; ?/// summary ?/// 表格标题 ?/// /summary ?private string title=null; ?/// summary ?/// 输出文件路径 ?/// /summary ?private string outFilePath=null; ?/// summary ?/// 输入文件名 ?/// /summary ?private string inputFilePath=null; ?#endregion ?#region 公共属性 ?/// summary ?/// 数据的DataView ?/// /summary ?public DataView DV ?{ ??set{dv=value;} ?} ?/// summary ?/// 表格标题 ?/// /summary ?public string Title ?{ ??set{title=value;} ??get{return title;} ?} ?/// summary ?/// 输出文件路径 ?/// /summary ?public string OutFilePath ?{ ??set{outFilePath=value;} ??get{return outFilePath;} ?} ?/// summary ?/// 输入文件路径 ?/// /summary ?public string InputFilePath ?{ ??set{inputFilePath=value;} ??get{return inputFilePath;} ?} ?#endregion ? ?#region 构造函数 ?public OutputExcel() ?{ ?} ?public OutputExcel(DataView dv,string title) ?{ ??// ??// TODO: 在此处添加构造函数逻辑 ??// ?} ?#endregion ?#region 公共方法 ?public void CreateExcel() ?{ ??int rowIndex=4;//行起始坐标 ??int colIndex=1;//列起始坐标 ??ApplicationClass myApp=null; ??Workbook myBook=null; ??Worksheet mySheet=null; ??//如果文件不存在,则将模板文件拷贝一份作为输出文件 ??//这里如果通过File.Create来创建文件是不行的,因为xls ??//的空文件也有固定的格式,跟文本不一样的,也许有其它 ??//通过程序直接生成excel的方法,大家可以尝试尝试的 ??if(!File.Exists(outFilePath)) ??{ ???File.Copy(inputFilePath,outFilePath,true); ??} ??myApp= new ApplicationClass(); ??myApp.Visible=false; ?

文档评论(0)

1亿VIP精品文档

相关文档