从 ASP.Net 添加页眉和页脚到 Word 文档

发布于 2024-07-19 02:09:34 字数 68 浏览 1 评论 0原文

有什么办法可以在Word中添加页眉和页脚信息。 我目前正在使用 ASP.Net 将 HTML 内容写入 Word 文件。

Is there any way to add Header and Footer Info to Word. I am currently writing HTML contents to word file using ASP.Net.

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

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

发布评论

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

评论(1

梦醒灬来后我 2024-07-26 02:09:34

下面的代码可以在不使用 HTML 格式的情况下生成 MS Word 文档 -

object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object novalue = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot";
object newTemplate = false;
object docType = 0;
object isVisible = true;

Microsoft.Office.Interop.Word.ApplicationClass questionClient = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word.Document questionDoc = questionClient.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
questionDoc.Activate();

//adding header
string logoPath = Server.MapPath("~//photos//logica_logo_black.JPG");
Microsoft.Office.Interop.Word.Shape logoCustom = null;
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
logoCustom = questionClient.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
logoCustom.Select(ref oMissing);
logoCustom.Name = "CustomLogo";
logoCustom.Left = -47;
logoCustom.Top = -19;

//adding footer
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
questionClient.ActiveWindow.Selection.Font.Name = "Verdana";
questionClient.ActiveWindow.Selection.Font.Size = 8;
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
questionClient.ActiveWindow.Selection.TypeText("For Logica Internal Use Only");
questionClient.ActiveWindow.Selection.TypeText("                                             ");
questionClient.ActiveWindow.Selection.TypeText("Page ");
questionClient.ActiveWindow.Selection.Fields.Add(questionClient.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);
questionClient.ActiveWindow.Selection.TypeText("                                                     ");
questionClient.ActiveWindow.Selection.TypeText(DateTime.Today.ToString("MM/dd/yyyy"));

//saving and closing the document
questionClient.Documents.Save(ref oMissing, ref oMissing);
questionClient.Quit(ref oMissing, ref oMissing, ref oMissing);

Code below can generate the MS Word document without using HTML format -

object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object novalue = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot";
object newTemplate = false;
object docType = 0;
object isVisible = true;

Microsoft.Office.Interop.Word.ApplicationClass questionClient = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word.Document questionDoc = questionClient.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
questionDoc.Activate();

//adding header
string logoPath = Server.MapPath("~//photos//logica_logo_black.JPG");
Microsoft.Office.Interop.Word.Shape logoCustom = null;
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
logoCustom = questionClient.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
logoCustom.Select(ref oMissing);
logoCustom.Name = "CustomLogo";
logoCustom.Left = -47;
logoCustom.Top = -19;

//adding footer
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
questionClient.ActiveWindow.Selection.Font.Name = "Verdana";
questionClient.ActiveWindow.Selection.Font.Size = 8;
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
questionClient.ActiveWindow.Selection.TypeText("For Logica Internal Use Only");
questionClient.ActiveWindow.Selection.TypeText("                                             ");
questionClient.ActiveWindow.Selection.TypeText("Page ");
questionClient.ActiveWindow.Selection.Fields.Add(questionClient.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);
questionClient.ActiveWindow.Selection.TypeText("                                                     ");
questionClient.ActiveWindow.Selection.TypeText(DateTime.Today.ToString("MM/dd/yyyy"));

//saving and closing the document
questionClient.Documents.Save(ref oMissing, ref oMissing);
questionClient.Quit(ref oMissing, ref oMissing, ref oMissing);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文