C#对 WORD 的控制集锦

首先步骤是:导入COM库:Microsoft word 11.0 Object Library.


创建新Word

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  7.                 ref oMissing, ref oMissing);

打开文档:

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             object fileName = @"E:CCCXCXXTestDoc.doc";
  7.             oDoc = oWord.Documents.Open(ref fileName,
  8.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  9.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  10.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

导入模板

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             object fileName = @"E:XXXCCXTest.doc";
  7.             oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
  8.                             ref oMissing, ref oMissing);

添加新表

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  7.                 ref oMissing, ref oMissing);
  8.             object start = 0;
  9.             object end = 0;
  10.             Word.Range tableLocation = oDoc.Range(ref start, ref end);
  11.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

表插入行

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  7.                 ref oMissing, ref oMissing);
  8.             object start = 0;
  9.             object end = 0;
  10.             Word.Range tableLocation = oDoc.Range(ref start, ref end);
  11.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
  12.             Word.Table newTable = oDoc.Tables[1];
  13.             object beforeRow = newTable.Rows[1];
  14.             newTable.Rows.Add(ref beforeRow);

单元格合并

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  7.                 ref oMissing, ref oMissing);
  8.             object start = 0;
  9.             object end = 0;
  10.             Word.Range tableLocation = oDoc.Range(ref start, ref end);
  11.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
  12.             Word.Table newTable = oDoc.Tables[1];
  13.             object beforeRow = newTable.Rows[1];
  14.             newTable.Rows.Add(ref beforeRow);
  15.             Word.Cell cell = newTable.Cell(1, 1);
  16.             cell.Merge(newTable.Cell(1, 2));

单元格分离

 

  1. object oMissing = System.Reflection.Missing.Value;
  2.             Word._Application oWord;
  3.             Word._Document oDoc;
  4.             oWord = new Word.Application();
  5.             oWord.Visible = true;
  6.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  7.                 ref oMissing, ref oMissing);
  8.             object start = 0;
  9.             object end = 0;
  10.             Word.Range tableLocation = oDoc.Range(ref start, ref end);
  11.             oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
  12.             Word.Table newTable = oDoc.Tables[1];
  13.             object beforeRow = newTable.Rows[1];
  14.             newTable.Rows.Add(ref beforeRow);
  15.             Word.Cell cell = newTable.Cell(1, 1);
  16.             cell.Merge(newTable.Cell(1, 2));
  17.             object Rownum = 2;
  18.             object Columnnum = 2;
  19.             cell.Split(ref Rownum, ref  Columnnum);

通过段落控制插入

  1. object oMissing = System.Reflection.Missing.Value;
  2.             object oEndOfDoc = "/endofdoc"/**//* endofdoc is a predefined bookmark */
  3.             //Start Word and create a new document.
  4.             Word._Application oWord;
  5.             Word._Document oDoc;
  6.             oWord = new Word.Application();
  7.             oWord.Visible = true;
  8.             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  9.                 ref oMissing, ref oMissing);
  10.             //Insert a paragraph at the beginning of the document.
  11.             Word.Paragraph oPara1;
  12.             oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
  13.             oPara1.Range.Text = "Heading 1";
  14.             oPara1.Range.Font.Bold = 1;
  15.             oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
  16.             oPara1.Range.InsertParagraphAfter();

常见操作

  1. bool saveChange = false;
  2.             object missing = System.Reflection.Missing.Value;
  3.             object template = (object)templateFilePath;
  4.             object filename = (object)saveFilePath;   
  5.             object isVisible = missing;
  6.             object readOnly = missing;
  7.             object breakType = Word.WdBreakType.wdSectionBreakNextPage;
  8.             object isSaveChange = (object)saveChange;
  9.             Word.Document doc = null;
  10.  
  11.             //定义一个Word.Application 对象
  12.             Word.Application WordApp =  new  Word.ApplicationClass();
  13. //打开文档
  14.                 doc = WordApp.Documents.Open(
  15.                     ref template, ref missing,ref readOnly,ref missing, ref missing, ref missing, 
  16.                     ref missing, ref missing, ref missing,ref missing, ref missing, ref isVisible, 
  17.                     ref missing, ref missing, ref missing, ref missing);
  18.  
  19.                 //设置页眉文本
  20.                 WordApp.ActiveWindow.ActivePane.View.SeekView 
  21.                     = Word.WdSeekView.wdSeekCurrentPageHeader;
  22.                 WordApp.Selection.WholeStory();
  23.                 WordApp.Selection.TypeText( this.m_titleText );
  24.                 WordApp.ActiveWindow.ActivePane.View.SeekView 
  25.                     = Word.WdSeekView.wdSeekMainDocument;
  26.  
  27.                 //页面设置,设置页面为纵向布局,设置纸张类型为A4纸
  28.                 doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
  29.                 doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
  30.                 doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
  31.  
  32.                 //创建表格及设置表格和单元格属性
  33.                 object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
  34.                 doc.Content.Tables.Add(
  35.                     WordApp.Selection.Range, 
  36.                     totalCount + 1, 
  37.                     totalField - keyCount_1, 
  38.                     ref missing, 
  39.                     ref autoFitBehavior);
  40.                 //合并单元隔
  41.                 doc.Content.Tables[1].Cell(i+1,j).Select();
  42.                                     object moveUnit = Word.WdUnits.wdLine;
  43.                                     object moveCount = 1;
  44.                                     object moveExtend = Word.WdMovementType.wdExtend;
  45.                                     WordApp.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
  46.                                     WordApp.Selection.Cells.Merge();
  47.                                     WordApp.Selection.Cells.VerticalAlignment =  Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  48.  
  49.                     doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”
  50.  
  51. //添加表格行
  52. doc.Content.Tables[0].Rows.Add(ref beforeRow);
  53. //添加表格列
  54. doc.Content.Tables[0].Columns.Add(ref beforeColumn);
  55. //文本居中
  56. WordApp.Selection.ParagraphFormat.Alignment =
  57.       Word.WdParagraphAlignment.wdAlignParagraphCenter;
  58. WordApp.Selection.Cells.VerticalAlignment =  
  59.       Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//选中单元格文字垂直居中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值