我希望构建一个类似于这个解决方案的系统:
http://esqinc.com/section/products/4/idocid.html
该系统的功能是将文档文件名插入到文档页脚中。如何通过编程实现这一点(最好使用.NET)?
foreach ( Section wordSection in wordDoc.Sections )
{
HeaderFooter footer = wordSection.Footers[ Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary ];
footer.Range.Select( );
footer.Range.Text = footerTxt;
hf.Range.Font.Size = 10;
wordApp.Selection.Paragraphs[ 1 ].Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
wordApp.Selection.Paragraphs[ 1 ].SpaceAfter = 0;
}
我碰巧正在处理一个从C#中在Excel中实现此功能的代码...下面是部分代码,可以帮助您入门...
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = true;
Microsoft.Office.Interop.Excel._Workbook book = (Microsoft.Office.Interop.Excel._Workbook)excelapp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); ;
Microsoft.Office.Interop.Excel._Worksheet sheet = (Microsoft.Office.Interop.Excel._Worksheet)book.ActiveSheet;
sheet.get_Range("A1", "N999").Font.Size = "8";
sheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperLegal;
sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
sheet.PageSetup.PrintTitleRows = "$3:$5";
sheet.PageSetup.PrintTitleColumns = "$A:$B";
这里的代码比你针对特定任务需要的更多,但是与拥有页眉(或在每个页面顶部重复出现的内容)相关的行是:
sheet.PageSetup.PrintTitleRows = "$3:$5";
sheet.PageSetup.PrintTitleColumns = "$A:$B";
编辑 - 添加
这是一个链接,提供了所有与Office Interop相关的MSDN文档。
http://msdn.microsoft.com/zh-cn/library/bb209015(office.12).aspx