一、使用c#向Word文档中添加表格
除了简单的文本信息外,Microsoft Word也是一个处理表格的强大工具。许多数据报表也需要通过表格的形式在Word中体现。本节将介绍如何使用c#在Word中创建一个表格。表格是由Microsoft Word X Object Library中的MSWord.Table定义的,通过在Word文档中的Tables集合中添加一个Table对象,即可在Word文档中创建一个表格。该表格的行数和列数等属性都可以在Tables的Add方法中定义,表格的内容可由Cell属性访问。
介绍如何向Word文档中输出表格和如何向Word文档中的表格填充文本。
添加对Microsoft Word 12.0 Object Library的引用
using MSWord =Microsoft.Office.Interop.Word;
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);
//定义一个Word中的表格对象
MSWor.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,5,5,ref Nothing,ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable=1;
//使用两层循环填充表格的内容
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
table.Cell(i,j).Range.Text="第"+i+"行,第"+j+"列";
}
}
//WdSaveFormat为Word2007文档的保存格式
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+"创建完毕!");
}
}
二、使用c#向Word文档中插入图片
要想创建一个完整美观的Word文档,图片是必不可少的。在COM组件Microsoft Word X Object Library中,图片是由MSWord.Document.InlineShapes.AddPicture负责添加的,而没有单独表示图片的对象。只需用AddPicture方法给出图片的物理地址及一些简单的属性即可向Word文档中添加图片。
添加对Microsoft Word 12.0 Object Library的引用
using MSWord=Microsoft.Office.Interop.Word;
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);
//图片文件的路径
string filename=@"c:\BackgroundImage.jpg";
//要向Word文档中插入图片的位置
Object range=wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile=false;//默认
//定义要插入的图片是否随Word文档一起保存
Object saveWithDocument=true;//默认
//使用InlineShapes.AddPicture方法插入图片
wordDoc.InlineShapes.AddPicture(filename,ref linkToFile,ref saveWithDocument,ref range);
//WdSaveFormat为Word2007文档的保存格式
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+"创建完毕!");
}
}