从ASP.NET导出的word文件中添加页眉/页脚

IND*_*ECH 6 vb.net asp.net ms-word

我的应用程序中有一个"导出到单词"功能.它完美地运作.我使用gridview的内容导出到word文件中.

现在我想在导出的word文件中添加页眉/页脚,该文件由以下代码生成:

Dim fileName As String = "Test_" & Format(DateTime.Now, "MMddyyyyhhmmss") & ".doc"
Dim sw As New StringWriter()
Dim w As New HtmlTextWriter(sw)
gvContent.RenderControl(w)
Dim content As String = sw.GetStringBuilder().ToString()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word"
Response.Write(finalContent)
Response.Flush()
Response.End()
Run Code Online (Sandbox Code Playgroud)

页眉和页脚也应该显示在word文件的所有页面中,就像使用Word的页眉/页脚功能一样.

可能吗?有没有人对此有所了解?

mas*_*son 2

您所做的实际上是创建一个 HTML 文件并为其指定一个 Word 可以打开的扩展名。您没有创建真正的 .DOC 文件,但 Word 将识别 和 中的 HTML 并显示它。

我怀疑它正在寻找的 HTML 风格与其保存的风格相同。因此,我在 Word 2013 中创建了一个新文档,添加了页眉和页脚,并将其另存为 HTML 文件。检查 HTML 文件后,Word 似乎忽略了这些内容。所以我怀疑是否有一种方法可以在它打开的 HTML 文件中指定页眉和页脚。

您可以做的是切换到生成真正的 MS Word 文件。这些将对各种 Word 客户端和 Word 等效项(例如 Mac 版本、移动版本和 Libre Office)提供更好的支持。

Micrsoft 提供了一个用于生成 .DOCX 文件的库,称为Open XML SDK。但是,我发现它有点难以使用。

我个人已经使用过几次DocX 。以下是使用该库完成此操作的方法(代码取自此博客文章):

C#

// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
    // Add Header and Footer support to this document.
    document.AddHeaders();
    document.AddFooters();

    // Get the default Header for this document.
    Header header_default = document.Headers.odd;

    // Get the default Footer for this document.
    Footer footer_default = document.Footers.odd;

    // Insert a Paragraph into the default Header.
    Paragraph p1 = header_default.InsertParagraph();
    p1.Append("Hello Header.").Bold();

    // Insert a Paragraph into the document.
    Paragraph p2 = document.InsertParagraph();
    p2.AppendLine("Hello Document.").Bold();

    // Insert a Paragraph into the default Footer.
    Paragraph p3 = footer_default.InsertParagraph();
    p3.Append("Hello Footer.").Bold();

    // Save all changes to this document.
    document.Save();
}// Release this document from memory.
Run Code Online (Sandbox Code Playgroud)

VB.NET(由 Telerik 翻译,因为我不了解 VB.NET)

' Create a new document.
Using document As DocX = DocX.Create("Test.docx")
    ' Add Header and Footer support to this document.
    document.AddHeaders()
    document.AddFooters()

    ' Get the default Header for this document.
    Dim header_default As Header = document.Headers.odd

    ' Get the default Footer for this document.
    Dim footer_default As Footer = document.Footers.odd

    ' Insert a Paragraph into the default Header.
    Dim p1 As Paragraph = header_default.InsertParagraph()
    p1.Append("Hello Header.").Bold()

    ' Insert a Paragraph into the document.
    Dim p2 As Paragraph = document.InsertParagraph()
    p2.AppendLine("Hello Document.").Bold()

    ' Insert a Paragraph into the default Footer.
    Dim p3 As Paragraph = footer_default.InsertParagraph()
    p3.Append("Hello Footer.").Bold()

    ' Save all changes to this document.
    document.Save()
End Using
' Release this document from memory.
Run Code Online (Sandbox Code Playgroud)

请注意,上面的代码取自 2010 年编写的一篇博客文章。该库可能在​​这六年中发生了变化。