8.2 使用C#向Word文档中写入文本
文本是一个Word文档中最简单的元素,通过各种形式的文本与其他元素有机组合才形成了一个完整的Word文档。本节介绍如何使用C#向Word文档中写入文本信息。
在向Word文档中写入文本时,仍然需要使用上节介绍的Microsoft Word X
Object Library
COM组件。写入文本的方法主要为设置MSWord.Document.Paragraphs.Last.Range.Text属性,通过设置不同的字符
串,即可达到写入文本的目的。
介绍如何向Word文档中写入文本和如何向Word文档中写入多行文本。
(1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。
(2)添加对Microsoft Word 12.0 Object Library的引用。
(3)在“Program.cs”文件中添加如下引用。
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
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 = "使用C#向Word文档中写入文本/n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "写入第二行文本";
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 + " 创建完毕!");
}
}
运行程序,结果如图8.9所示。
图8.9 运行结果
打开C盘根目录下的MyWord.docx,如图8.10所示。
图8.10 运行结果
8.3 使用C#向Word输出格式化的文本
一个Word文档不可能全部由无格式的普通文本组成,因此在从C#中向Word写入文本信息时经常要输出一些具有特殊字体、颜色的文本。本节介绍如何向Word输出格式化的文本。
Microsoft Word X Object Library COM组件中文本格式的设置主要是由文本所使用的字体决定的。该COM组件中可以直接设置C#中的Font类,使用非常方便。常用的格式属性有颜色、加粗、斜体、下画线等。
分别介绍以下内容:
— 输出不同字体的文本。
— 输出不同颜色的文本。
— 输出带下画线的文本。
— 输出斜体文本。
— 输出加粗文本。
(1)创建一个Windows控制台应用程序,命名为CreateFormatWordDemo。
(2)添加对Microsoft Word 12.0 Object Library的引用。
(3)在“Program.cs”文件中添加如下引用。
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
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 = "黑体文本黑体文本黑体文本黑体文本黑体文本/n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入加粗文本
strContent = "加粗文本加粗文本加粗文本加粗文本加粗文本/n";
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入15号字体文本
strContent = "15号字体文本15号字体文本15号字体文本15号字体文本/n";
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入斜体文本
strContent = "斜体文本斜体文本斜体文本斜体文本斜体文本/n";
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入蓝色文本
strContent = "蓝色文本蓝色文本蓝色文本蓝色文本蓝色文本/n";
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入下画线文本
strContent = "下画线文本下画线文本下画线文本下画线文本下画线文本/n";
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入红色下画线文本
strContent = "红色下画线文本红色下画线文本红色下画线文本红色下画线文本/n";
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorRed;
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 + " 创建完毕!");
}
}
运行程序,结果如图8.11所示。
打开C盘根目录下的MyWord.docx,如图8.12所示。
图8.11 运行结果 图8.12 运行结果
8.4 使用C#向Word文档中添加表格
除了简单的文本信息外,Microsoft Word也是一个处理表格的强大工具。许多数据报表也需要通过表格的形式在Word中体现。本节将介绍如何使用C#在Word中创建一个表格。
表格是由Microsoft Word X Object
Library中的MSWord.Table定义的,通过在Word文档中的Tables集合中添加一个Table对象,即可在Word文档中创建一个表
格。该表格的行数和列数等属性都可以在Tables的Add方法中定义,表格的内容可由Cell属性访问。
介绍如何向Word文档中输出表格和如何向Word文档中的表格填充文本。
(1)创建一个Windows控制台应用程序,命名为CreateTableDemo。
(2)添加对Microsoft Word 12.0 Object Library的引用。
(3)在“Program.cs”文件中添加如下引用。
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
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中的表格对象
MSWord.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为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 + " 创建完毕!");
}
}
运行程序,结果如图8.13所示。
打开C盘根目录下的MyWord.docx,如图8.14所示。
图8.13 运行结果 图8.14 运行结果
相关推荐
C#的office文档操作.rar C#的office文档操作.rar C#的office文档操作.rar
用C#操作Office等办公软件,如Excel、Word、Text、Xml文档的创建,读取,写入,保存等,来进行数据的保存与读取。
C#使用npoi操作office案例C#使用npoi操作office案例C#使用npoi操作office案例C#使用npoi操作office案例C#使用npoi操作office案例C#使用npoi操作office案例
通过C#代码对office文档,表格,wps进行简单操作,完成在web端对文档的常见操作(操作是需要安装指定浏览器,详情见提示,文档)
DsoFramer接口文档 ,WPF 结合DsoFramer嵌入Office应用程序, 操作word文档, excel文档 ,
C#在iis上发布网站,从网站中导出excel需要配置office权限
C# 在线编辑Office源码及其文档收集了多个代码,比较全,下了这个其他的都不用下载了。
通过C#程序控制wps自动转换office文档转为pdf文件
c#语言将office文档转为XPS,核心代码已经给出。
C# winform 使用第三方软件DsoFramer 创建 显示office 文档, office文档嵌入winform窗体内进行编辑。
C#的office操作,包括对word,excel,powerpoint的创建文档,插入文字,插入表格,图片等等操作
c#写的在form上打开并显示OFFICE文档
C#使用XML创建Excel文档,无需安装office
没有比这个功能更完全的 有详细的安装说明和示例说明,1。解压缩软件包到某个目录,比如:d:\v30CSharpdemoSQL 2。...这将使得 .net服务器允许服务NTKO OFFICE文档控件加密的印章文件类型。
c# 操作Office文件大全各种代码 操作Word文档; 操作PDF文档 ; 操作PowerPoint文档; 操作Excel工作簿;
网上搜到之前的嵌入Winform代码已经不可用了,自己搜索也没有找到,参考官方API文档自己弄了一个,基于LibreOffice 7.2.5,兼容性不错,绝大部分Office文档都可以浏览 且可以转换成PDF,自由嵌入自己的WinForm窗体,...
不需要安装Office,实现Word文档转换为图片。C#,VB.NET 都可以用。
很有用的一个教材,主要详细介绍如何使用C#操作(文件的创建和写入)各类Office文件,包括有word,ppt,excel pdf等文档类型。
c#实现 office(doc xls ppt等)转pdf文档代码类,代码中如有不合理的希望多多指教
C#使用Microsoft.Office.Interop.Excel.dll读写表格