C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass()
word对象
2、 Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing) 文档对象
3、 Object Nothing = System.Reflection.Missing.Value 常用的一个参数
4、wordApp.ActiveWindow.ActivePane.View.SeekView = WdSeekView.
wdSeekCurrentPageHeader定位到页眉
5、 wordApp.Selection.ParagraphFormat. Borders[WdBorderType.wdBorderBottom]
.LineStyle = WdLineStyle.wdLineStyleNone去掉页眉线
6、wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument跳出页眉
7、 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.
wdAlignParagraphCenter对齐方式(此为居中)
8、 Microsoft.Office.Interop.Word.Table policyTab = wordDoc.Tables.Add
(wordApp.Selection.Range, 行数, 列, ref Nothing, ref Nothing);创建表格
9、 wordApp.Selection.GoTo(ref oGoToItem, ref oGoToLast, ref Nothing, ref Nothing);定位页面(此为将光标定位到内容的最后)
10、 wordApp.Selection.InsertBreak(ref Nothing);插入分节符
11、 wordApp.Selection.InsertFile(nofile, ref Nothing, ref contenFalse, ref contenFalse, ref contenFalse);插入文件
12、 wordApp.Selection.InlineShapes.AddPicture(picName, ref LinkToFile, ref SaveWithDocument, ref Nothing);插入图片
13、 wordApp.ActiveWindow.ActivePane.View.SeekView = WdSeekView.
wdSeekCurrentPageFooter;定位到页脚
14、 wordApp.Selection.TypeText("此处写需要在指定位置输出的文字");文本输入
15、 wordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);得到页码
16、 wordDoc.PrintOut(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, ref Nothing,
ref Nothing, ref Nothing, ref Nothing);
调用打印机实现打印(调用的是默认打印机)
17、 wordDoc.Close(ref saveChanges, ref Nothing, ref Nothing);文档退出
18、 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);word程序退出
doc.pageSetip.OddAndEvenPagesHeaderFooter=-1;//奇偶页不同
//去掉页脚
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text="";
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.Text="";
//页码
object leftAlign=WdPageNumberAlignment.wdAlignPageNumberInside;
object rightAlign=WdPageNumberAlignment.wdAlignPageNumberoutside;
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref leftAlign);
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterEvenpages].PageNumbers.Add(ref rightAlign);
// 页码样式“- 1 -”
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterEvenpages].PageNumbers.NumberStyle=WdPageNumberStyle.wdPageNumberStyleNumberInDash;
//页码的字号字体颜色
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size=14;
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Bold=0;
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Color=Word.WdColor.wdColorBlack;
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Name="宋体";
//去掉页眉横线
doc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs[1].Format.Borders[WdBorderType.wdBorderBottom].Color=WdColor.wdColorBlack;
doc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs[1].Format.Borders[WdBorderType.wdBorderBottom].LineStyle=WdLineStyle.wdLineStyleNone;
doc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs[1].Format.Borders[WdBorderType.wdBorderBottom].LineWidth=WdLineWidth.wdLineWidth050pt;