以编程方式向Office Word/Excel文档添加页脚

6
2个回答

6
希望这能帮助您入门。以下伪代码可用于向页脚添加文本,但您需要在宏中执行此操作以完全自动化,并识别要添加的文档名称。最后,在文档保存期间调用宏以添加页脚文本。
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;
}

太酷了!我不知道怎么在Word里做,但那是因为我以前从未遇到过。我会收藏这个页面以便查看你的答案,因为我知道以后会需要。+1。 - David

4

我碰巧正在处理一个从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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接