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

发布于 2024-09-13 04:38:57 字数 218 浏览 6 评论 0原文

我正在寻求构建一个与此类似的解决方案: http://esqinc.com/section/products/4/idocid.html

系统所做的是将文档文件名插入到文档页脚中。这如何以编程方式实现(最好是在 .NET 中)?

I'm looking to build a solution similar to this one:
http://esqinc.com/section/products/4/idocid.html

What the system makes is insertion of a document file name into the document footer. How's that possible programmatically (preferably in .NET)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-09-20 04:38:57

希望这能让你开始。以下伪 C# 代码可用于向页脚添加文本。只有您必须在宏中执行此操作才能完全自动化,并确定要添加的文档名称。最后在文档保存期间调用宏以添加页脚文本。

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

Hoping this gets you started. The following pseudo c# code can be used to add text to footer. Only you will have to do this in a macro to completely automate this and also identify the document name to be added. Finally call in to the macro during Document Save to add the footer text.

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;
}
往事随风而去 2024-09-20 04:38:57

我只是碰巧正在编写代码,我已经在 Excel 中从 C# 执行此操作...这是部分的,并且将帮助您开始...

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

编辑 - 添加

这里是 MSDN 文档的链接,可满足您的所有 Office Interop 需求。

http://msdn.microsoft.com/en-us /library/bb209015(office.12).aspx

I just happened to be working on code where I already do this in Excel from C#... This is partial, and will get you started...

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

There's more code there than you need for this specific task, but the relevant lines for having a header (or something that repeats at the top of each page) are:

sheet.PageSetup.PrintTitleRows = "$3:$5";
sheet.PageSetup.PrintTitleColumns = "$A:$B";

Edit - Added

Here's a link to MSDN documentation, for all your Office Interop needs.

http://msdn.microsoft.com/en-us/library/bb209015(office.12).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文