
建立一个自己的类
//word 类,
public class YFCWord
{
public void testword()
{
object path; //文件路径变量
string strContent; //文本内容变量
Word.Application wordApp; //Word应用程序变量
Word.Document wordDoc; //Word文档变量
path = Environment.CurrentDirectory + "\测试文件.doc";
wordApp = new Word.ApplicationClass(); //初始化
wordApp.Visible = true;//使文档可见
//如果已存在,则删除
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);
#region 页面设置、页眉图片和文字设置,最后跳出页眉设置
//页面设置
wordDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
wordDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
wordDoc.PageSetup.TopMargin = 57.0f;
wordDoc.PageSetup.BottomMargin = 57.0f;
wordDoc.PageSetup.LeftMargin = 57.0f;
wordDoc.PageSetup.RightMargin = 57.0f;
wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
Thread.Sleep(10);;
//设置页眉
//wordApp.ActiveWindow.View.Type = Word.WdViewType.wdNormalView;//普通视图(即页面视图)样式
wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;//进入页眉设置,其中页眉边距在页面设置中已完成
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//页眉中的文字右对齐
Thread.Sleep(10);;
//插入页眉图片(测试结果图片未插入成功)
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
string headerfile = Environment.CurrentDirectory + @"华智电气.png";
Word.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
shape1.Height = 15;//强行设置貌似无效,图片没有按设置的缩放——图片的比例并没有改变。
shape1.Width = 60;
wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" 测试记录");//在页眉的图片后面追加几个字
//去掉页眉的横线
wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleNone;
wordApp.ActiveWindow.ActivePane.Selection.Borders[Word.WdBorderType.wdBorderBottom].Visible = false;
wordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//退出页眉设置
#endregion
Thread.Sleep(10);;
#region 页码设置并添加页码
//为当前页添加页码
Word.PageNumbers pns = wordApp.Selection.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
pns.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;//设置页码的风格,是Dash形还是圆形的
pns.HeadingLevelForChapter = 0;
pns.IncludeChapterNumber = false;
pns.RestartNumberingAtSection = false;
pns.StartingNumber = 0; //开始页页码?
object pagenmbetal = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
object first = true;
wordApp.Selection.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
#endregion
Thread.Sleep(10);;
#region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置
wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距
wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
//写入普通文本
strContent = "我是普通文本n wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距nwordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
wordDoc.Paragraphs.Last.Range.Text = "我再加一行试试,这里不加'\n'";
//直接添加段,不是覆盖( += )
wordDoc.Paragraphs.Last.Range.Text += "不会覆盖的,";
Thread.Sleep(10);;
//添加在此段的文字后面,不是新段落
wordDoc.Paragraphs.Last.Range.InsertAfter("这是后面的内容n wordDoc.Paragraphs.Last.Range.InsertAftern");
Thread.Sleep(10);;
//将文档的前4个字替换成"哥是替换文字",并将其颜色设为红色
object start = 0;
object end = 4;
Word.Range rang = wordDoc.Range(ref start, ref end);
rang.Font.Color = Word.WdColor.wdColorRed;
rang.Text = "这是替换文字";
wordDoc.Range(ref start, ref end);
Thread.Sleep(10);;
//写入黑体文本
object unite = Word.WdUnits.wdStory;
wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
strContent = "取消首行缩进,这是黑体文本n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入加粗文本
strContent = "这是粗体文本n"; //
wordApp.Selection.EndKey(ref unite, ref Nothing);//这一句不加,有时候好像也不出问题,不过还是加了安全
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入15号字体文本
strContent = "我这个文本的字号是15号,而且是宋体n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入斜体文本
strContent = "我是斜体字文本n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入蓝色文本
strContent = "我是蓝色的文本n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Color = Word.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入下划线文本
strContent = "我是下划线文本n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = Word.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//写入红色下画线文本
strContent = "我是点线下划线,并且下划线是红色的n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = Word.WdUnderline.wdUnderlineDottedHeavy;
wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = Word.WdColor.wdColorRed;
wordDoc.Paragraphs.Last.Range.Text = strContent;
Thread.Sleep(10);;
//取消下划线,并且将字号调整为12号
strContent = "我不要下划线了,并且设置字号为12号,黑色不要斜体n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 12;
wordDoc.Paragraphs.Last.Range.Font.Underline = Word.WdUnderline.wdUnderlineNone;
wordDoc.Paragraphs.Last.Range.Font.Color = Word.WdColor.wdColorBlack;
wordDoc.Paragraphs.Last.Range.Font.Italic = 0;
wordDoc.Paragraphs.Last.Range.Text = strContent;
#endregion
Thread.Sleep(10);;
#region 插入图片、居中显示,设置图片的绝对尺寸和缩放尺寸,并给图片添加标题
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
//图片文件的路径
string filename = Environment.CurrentDirectory + "\3.jpg";
//要向Word文档中插入图片的位置
Object range = wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile = false; //默认,这里貌似设置为bool类型更清晰一些
//定义要插入的图片是否随Word文档一起保存
Object saveWithDocument = true; //默认
//使用InlineShapes.AddPicture方法(【即“嵌入型”】)插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片
//设置图片宽高的绝对大小
//wordDoc.InlineShapes[1].Width = 200;
//wordDoc.InlineShapes[1].Height = 150;
//按比例缩放大小
wordDoc.InlineShapes[1].ScaleWidth = 20;//缩小到20% ?
wordDoc.InlineShapes[1].ScaleHeight = 20;
//在图下方居中添加图片标题
wordDoc.Content.InsertAfter("n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Selection.Font.Size = 10;//字体大小
wordApp.Selection.TypeText("图1 测试图片n");
#endregion
Thread.Sleep(10);;
#region 添加第一个表格 填充数据、设置表格行列宽高、合并单元格、添加表头斜线、给单元格添加图片
wordDoc.Content.InsertAfter("n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
//object WdLine2 = Word.WdUnits.wdLine;//换一行;
//wordApp.Selection.MoveDown(ref WdLine2, 6, ref Nothing);//向下跨15行输入表格,这样表格就在文字下方了,不过这是非主流的方法
Thread.Sleep(10);;
//设置表格的行数和列数
int tableRow = 6;
int tableColumn = 6;
//定义一个Word中的表格对象
Word.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等
Thread.Sleep(10);;
//表格的索引是从1开始的。
wordDoc.Tables[1].Cell(1, 1).Range.Text = "列n行";
for (int i = 1; i < tableRow; i++)
{
for (int j = 1; j < tableColumn; j++)
{
if (i == 1)
{
table.Cell(i, j + 1).Range.Text = "Column " + j;//填充每列的标题
}
if (j == 1)
{
table.Cell(i + 1, j).Range.Text = "Row " + i; //填充每行的标题
}
table.Cell(i + 1, j + 1).Range.Text = i + "行 " + j + "列"; //填充表格的各个小格子
}
}
Thread.Sleep(10);;
//添加行
table.Rows.Add(ref Nothing);
table.Rows[tableRow + 1].Height = 35;//设置新增加的这行表格的高度
//向新添加的行的单元格中添加图片
string FileName = Environment.CurrentDirectory + "\3.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = table.Cell(tableRow + 1, tableColumn).Range;//选中要添加图片的单元格
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
Thread.Sleep(10);;
//由于是本文档的第2张图,所以这里是InlineShapes[2]
wordDoc.Application.ActiveDocument.InlineShapes[2].Width = 50;//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[2].Height = 35;//图片高度
Thread.Sleep(10);;
// 将图片设置为四周环绕型
Word.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[2].ConvertToShape();
s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
Thread.Sleep(10);;
//设置table样式
table.Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;//高度规则是:行高有最低值下限?
table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));//
table.Range.Font.Size = 10.5F;
table.Range.Font.Bold = 0;
table.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//表格文本居中
table.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//文本垂直贴到底部
//设置table边框样式
table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;//表格外框是双线
table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格内框是单线
table.Rows[1].Range.Font.Bold = 1;//加粗
table.Rows[1].Range.Font.Size = 12F;
table.Cell(1, 1).Range.Font.Size = 10.5F;
wordApp.Selection.Cells.Height = 30;//所有单元格的高度
Thread.Sleep(10);;
//除第一行外,其他行的行高都设置为20
for (int i = 2; i <= tableRow; i++)
{
table.Rows[i].Height = 20;
}
Thread.Sleep(10);;
//将表格左上角的单元格里的文字(“行” 和 “列”)居右
table.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
//将表格左上角的单元格里面下面的“列”字移到左边,相比上一行就是将ParagraphFormat改成了Paragraphs[2].Format
table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
table.Columns[1].Width = 50;//将第 1列宽度设置为50
//将其他列的宽度都设置为75
for (int i = 2; i <= tableColumn; i++)
{
table.Columns[i].Width = 75;
}
Thread.Sleep(10);;
//添加表头斜线,并设置表头的样式
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].Visible = true;
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].Color = Word.WdColor.wdColorRed;
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Word.WdLineWidth.wdLineWidth150pt;
Thread.Sleep(10);;
//合并单元格
table.Cell(4, 4).Merge(table.Cell(4, 5));//横向合并
table.Cell(2, 3).Merge(table.Cell(4, 3));//纵向合并,合并(2,3),(3,3),(4,3)
wordDoc.Content.InsertAfter("n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Selection.Font.Size = 10;//字体大小
wordApp.Selection.TypeText("表格1 测试表格n");
#endregion
#region 添加第二个表格 填充数据、设置表格行列宽高、合并单元格、添加表头斜线、给单元格添加图片
wordDoc.Content.InsertAfter("n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
//object WdLine2 = Word.WdUnits.wdLine;//换一行;
//wordApp.Selection.MoveDown(ref WdLine2, 6, ref Nothing);//向下跨15行输入表格,这样表格就在文字下方了,不过这是非主流的方法
Thread.Sleep(10);;
//设置表格的行数和列数
tableRow = 6;
tableColumn = 6;
//定义一个Word中的表格对象
table = wordDoc.Tables.Add(wordApp.Selection.Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等
Thread.Sleep(10);;
//表格的索引是从1开始的。
wordDoc.Tables[2].Cell(1, 1).Range.Text = "列n行";
for (int i = 1; i < tableRow; i++)
{
for (int j = 1; j < tableColumn; j++)
{
if (i == 1)
{
table.Cell(i, j + 1).Range.Text = "Column " + j;//填充每列的标题
}
if (j == 1)
{
table.Cell(i + 1, j).Range.Text = "Row " + i; //填充每行的标题
}
table.Cell(i + 1, j + 1).Range.Text = i + "行 " + j + "列"; //填充表格的各个小格子
}
}
Thread.Sleep(10);;
//添加行
table.Rows.Add(ref Nothing);
table.Rows[tableRow + 1].Height = 35;//设置新增加的这行表格的高度
Thread.Sleep(10);;
//设置table样式
table.Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;//高度规则是:行高有最低值下限?
table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));//
table.Range.Font.Size = 10.5F;
table.Range.Font.Bold = 0;
//table.Range.Font.TextColor = Word.WdColor.wdColorRed;
table.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//表格文本居中
//table.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//文本垂直贴到底部
//设置table边框样式
table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;//表格外框是双线
table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格内框是单线
table.Rows[1].Range.Font.Bold = 1;//字体加粗
table.Rows[1].Range.Font.Size = 12F;
table.Rows[1].Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorBlueGray;//设置行背景颜色
table.Rows[1].Range.Shading.ForegroundPatternColor = Word.WdColor.wdColorDarkYellow;//设置行前景色
table.Columns[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorDarkTeal;//设置列背景色
table.Columns[1].Shading.ForegroundPatternColor = Word.WdColor.wdColorGray375;//设置列前景色
table.Cell(1, 1).Range.Font.Size = 10.5F;
wordApp.Selection.Cells.Height = 30;//所有单元格的高度
Thread.Sleep(10);;
//除第一行外,其他行的行高都设置为20
for (int i = 2; i <= tableRow; i++)
{
table.Rows[i].Height = 20;
}
Thread.Sleep(10);;
//将表格左上角的单元格里的文字(“行” 和 “列”)居右
table.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
//将表格左上角的单元格里面下面的“列”字移到左边,相比上一行就是将ParagraphFormat改成了Paragraphs[2].Format
table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
table.Columns[1].Width = 50;//将第 1列宽度设置为50
//将其他列的宽度都设置为75
for (int i = 2; i <= tableColumn; i++)
{
table.Columns[i].Width = 75;
}
Thread.Sleep(10);;
//添加表头斜线,并设置表头的样式
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].Visible = true;
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].Color = Word.WdColor.wdColorRed;
table.Cell(1, 1).Borders[Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Word.WdLineWidth.wdLineWidth150pt;
Thread.Sleep(10);;
//合并单元格
table.Cell(4, 4).Merge(table.Cell(4, 5));//横向合并
table.Cell(2, 3).Merge(table.Cell(4, 3));//纵向合并,合并(2,3),(3,3),(4,3)
wordDoc.Content.InsertAfter("n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Selection.Font.Size = 10;//字体大小
wordApp.Selection.TypeText("表格2 测试表格n");
#endregion
Thread.Sleep(10);;
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordDoc.Content.InsertAfter("n");
wordDoc.Content.InsertAfter("就写这么多,算了吧!2020.4.20日 yufuchang");
Thread.Sleep(10);;
//WdSaveFormat为Word 2003文档的保存格式
object format = Word.WdSaveFormat.wdFormatDocument;// office 2007就是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 + " 创建完毕!");
// Console.ReadKey();
Thread.Sleep(10);;
//我还要打开这个文档玩玩
Word.Application app = new Word.Application();
Word.Document doc = null;
try
{
object unknow = Type.Missing;
app.Visible = true;
string str = Environment.CurrentDirectory + "\测试文件.doc";
object file = str;
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
string temp = doc.Paragraphs[1].Range.Text.Trim();
Console.WriteLine("你他妈输出temp干嘛?");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
wordDoc = doc;
wordDoc.Paragraphs.Last.Range.Text += "我真的不打算再写了,就写这么多吧";
}
///
/// 模板模式,在word文档中插入数据,需要提前设计好模板
///
public void testword_template()
{
object path; //文件路径变量
string strContent; //文本内容变量
Word.Application wordApp; //Word应用程序变量
Word.Document wordDoc; //Word文档变量
Thread.Sleep(2000);
path = Environment.CurrentDirectory + "\个人简历.dotx";
wordApp = new Word.ApplicationClass(); //初始化
wordApp.Visible = true;//使文档可见
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref path, ref Nothing, ref Nothing, ref Nothing);
object[] oBookMark = new object[5];
//赋值书签名
oBookMark[0] = "beizhu";
oBookMark[1] = "name";
oBookMark[2] = "sex";
oBookMark[3] = "birthday";
oBookMark[4] = "jiguan";
//赋值任意数据到书签的位置
wordDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "使用模板实现Word生成";
Thread.Sleep(2000);
wordDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = "李四";
Thread.Sleep(2000);
wordDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = "女";
Thread.Sleep(2000);
wordDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = "1987.06.07";
Thread.Sleep(2000);
wordDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = "保定";
Thread.Sleep(2000);
Thread.Sleep(10); ;
//WdSaveFormat为Word 2003文档的保存格式
object format = Word.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
//将wordDoc文档对象的内容保存为DOCX文档
path = Environment.CurrentDirectory + "\个人简历.doc";
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);
//wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭word
//wordDoc.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
包含2个函数,一个是在新文本的基础上自动添加内容,具体添加哪些内容,参考函数testword, 使用方法是
YFCWord yfcword = new YFCWord();
yfcword.testword();
第二个函数是在原有的word中按位置插入内容,
YFCWord yfcword = new YFCWord();
yfcword.testword_template();
第二个函数需要一个文件 个人简历.dotx 文件格式如下
需要在相应的位置增加书签,程序会根据书签填写内容