C#操作Office文件(四)

一、使用C#创建Excel文档

        Microsoft Excel 是Microsoft Office的一个组件,是功能强大的电子表格处理软件,它与文本处理软件的差别在于它能够运算复杂的公式,并且有条理地显示结果。Microsoft Excel是除了Microsoft Word之外最常用的办公软件之一,本节介绍如何使用C#创建Excel文档。

        与在C#中添加Word文档的方法类似,添加Excel文档时需要为项目添加对Microsoft Excel X Object Library的引用,其中的X对应为版本号。Excel 2007对应12.0.在Microsoft Excel X Object Library中,一个Excel文档由MSExcel.Workbook表示。

        本实例介绍如何创建Excel文档和如何使用不同格式保存创建的Excel文档

添加引用,引用的库位于"COM"选项卡下,名称为Microsoft Excel 12.0 Object Library,12.0对应2007,11,0对应2003;添加后在“解决方案资源管理器"面板的引用项中自动多出了三个引用,分别为Microsoft.Office.Core、Microsoft.Office.Interop.Excel和VBIDE。

using MSExcel=Microsoft.Office.Interop.Excel;

using System.IO;

using System.Reflection;

class Program

{

       static void Main(string[]  args)

       {

            object  path;//文件路径变量

            MSExcel.Application  excelApp;//Exel应用程序变量

            MSExcel.Workbook  excelDoc;//Excel文档变量

            path=@"c:\MyExcel.xlsx";//路径

            excelApp=new MSExcel.ApplicationClass();//初始化

            //如果已存在,则删除

            if(File.Exists((string)path))

            {

                  File.Delete((string)path);

            }

            //由于使用的是COM库,因此有许多变量需要用Nothing代替

            Object  Nothing=Missing.Value;

            excelDoc=excelApp.Workbooks.Add(Nothing);

            //WdSaveFormat为Excel文档的保存格式

            object  format=MSExcel.XlFileFormat.xlWorkbookDefault;

            //将excelDoc文档对象的内容保存为XLSX文档

            excelDoc.SaveAs(path,Nothing,Nothing,Nothing,Nothing,Nothing,MSExcel.XlSaveAsAccessMode.xlExclusive,Nothing,Nothing,Nothing,Nothing,Nothing);

             //关闭excelDoc文档对象

             excelDoc.Close(Nothing,Nothing,Nothing);

             //关闭excelApp组件对象

             excelApp.Quit();

             Console.WriteLine(path + "创建完毕!");

       }

}


可以看到,已经成功地创建了一个名为MyExcel.xlsx的Excel文档。该文档是Microsoft Excel 2007默认的文档格式,大小约为8KB.下面介绍如何使用其创建一个Microsoft Excel 2007中可以识别的其他格式的Excel文档。

在Microsoft.Office.Interop.Excel命名空间下有一个枚举名为XlFileFormat,设定了可用于保存的形式,XFileFormat枚举中定义的格式更为详细,下面介绍如何创建一个CSV格式的文档。

添加对Microsoft.Excel 12.0 Object Library的引用

using MSExcel=Microsoft.Office.Interop.Excel;

using System.IO;

using System.Reflection;

class Program

{

       static void Main(string[]  args)

       {

             object  path;//文件路径变量

              string   strContent;//文本内容变量

              MSWord.Application wordApp;//Word应用程序变量

              MSWord.Document   wordDoc;//Word文档变量

             path=@"c:\MyWord.docx";//路径

             wordApp=new MSWord.ApplicationClass();//初始化

             //如果已存在,则删除

             if(File.Exists((string)path))

             {

                  File.Delete((string)path);

             }

             //由于使用的是COM库,因此有许多变量需要用Missing.Value代替

             Object Nothing =Missing.Value;

             wordDoc=wordApp.Documents.Add(ref  Nothing,ref Nothing,ref Nothing,ref Nothing);

             //strContent="你好!\n";

             //wordDoc.Paragraphs.Last.Range.Text=strContent;

             //strContent="Hello World";

             //wordDoc.Paragraphs.Last.Range.Text=strContent;

             //WdSaveFormat为Word 2007文档的保存格式

              object  format=MSWord.WdSaveFormat.wdFormatDocumentDefault;


               //将wordDoc文档对象的内容保存为DOCX文档

               wordDoc.SaveAs(ref path,ref format,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);

               //关闭wordDoc文档对象

               wordDoc.Close(ref  Nothing,ref  Nothing,ref  Nothing);

               //关闭wordApp组件对象

               wordApp.Quit(ref  Nothing,ref  Nothing,ref  Nothing);

               Console.WriteLine(path+"创建完毕!");

       }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值