C# 操作word文档

 

动态创建Word文档

  object path;                                    //文件路径变量

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

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

path = @"C:\TeacherEmploy.doc";              //路径

  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);

设置文档宽度
wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2"));  // 左边缘距离

wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11;

wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));  // 右边缘距离

Object start = Type.Missing;

Object end = Type.Missing;

Object unit = Type.Missing;

Object count = Type.Missing;

wordDoc.Range(ref start, ref end).Delete(ref unit, ref count);

 

在文档的第一行添加文本的时候,不要指定start和end的值,直接用缺省的Nothing

// 标题

string strTitle = "盐城师范学院04-07学年专业技术人员聘期任务书";

wordDoc.Range(ref start, ref end).InsertBefore(strTitle);

// 标题样式设置

wordDoc.Range(ref start, ref end).Font.Name = "Verdana";

wordDoc.Range(ref start, ref end).Font.Bold = 1;

wordDoc.Range(ref start, ref end).Font.Size = 17f;

wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment =

MSWord.WdParagraphAlignment.wdAlignParagraphCenter;

range指定在标题之后添加


// 添加表格

object location = strTitle.Length;

MSWord.Range range = wordDoc.Range(ref location, ref location);

MSWord.Table table = wordDoc.Tables.Add(range, 4, 2, ref Nothing, ref Nothing); 

Tables是Word文档中所有表格的集合,通过索引访问每张表

// 表格样式设置

wordDoc.Tables[1].Range.Font.Name = "仿宋";

wordDoc.Tables[1].Range.Font.Bold = 0;

wordDoc.Tables[1].Range.Font.Size = 12f;

wordDoc.Tables[1].Range.ParagraphFormat.Alignment =

Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

// 定位单元格

Cell(i,j)实现定位单元格(单元格的索引如下图所示)

 

单元格的合并与拆分

object Rownum = 1;

object Columnnum = 5;

table.Cell(1, 2).Split(ref Rownum, ref Columnnum); // 拆分

table.Cell(6,2).Merge(table.Cell(6,3)); // 合并

水平居中与垂直居中

table.Cell(8,1).Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  // 垂直居中

table.Cell(8,1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;  // 水平居中

保存Word文档与释放资源

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

object format = MSWord.WdSaveFormat.wdFormatDocument;

 //将wordDoc文档对象的内容保存为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.Paragraphs  // 段落集合

wordDoc.Tables       // 表格集合

如果直接在wordDoc.Paragraphs和wordDoc.Tables操作,则效果将反应在所有表格和所有段落中

通过索引可以访问每一个段落和每一张表格,注意,索引是基于1的!

原文地址:https://www.cnblogs.com/CPFlying/p/1685737.html