以编程方式更改Word文档的版本?

发布于 2024-10-06 04:52:43 字数 4571 浏览 0 评论 0原文

我正在动态生成一个 Word 文件,单击链接将打开文件保存对话框,它显示该文档是:Microsoft Word 97 - 2003 文档。

我需要做什么才能以编程方式生成 2007 年、2010 年的 Word 文档。

背后代码:

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    'build the content for the dynamic Word document
    'in HTML alongwith some Office specific style properties. 

    Dim strBody As New System.Text.StringBuilder("")

    strBody.Append("<html " & _
                    "xmlns:o='urn:schemas-microsoft-com:office:office' " & _
                    "xmlns:w='urn:schemas-microsoft-com:office:word'" & _
                    "xmlns='http://www.w3.org/TR/REC-html40'>" & _
                    "<head><title></title>")

    'The setting specifies document's view after it is downloaded as Print Layout
    'instead of the default Web Layout. For the Header & Footer to be visible,
    'this mode is required.

    strBody.Append("<!--[if gte mso 9]>" & _
                    "<xml>" & _
                    "<w:WordDocument>" & _
                    "<w:View>Print</w:View>" & _
                    "<w:Zoom>90</w:Zoom>" & _
                    "<w:DoNotOptimizeForBrowser/>" & _
                    "</w:WordDocument>" & _
                    "</xml>" & _
                    "<![endif]-->")

    'we can tweak the MsoFooter class that is referenced by the footer, as required

    strBody.Append("<style>" & _
                    "<!-- /* Style Definitions */" & _
                    "p.MsoFooter, li.MsoFooter, div.MsoFooter" & _
                    "{margin:0in;" & _
                    "margin-bottom:.0001pt;" & _
                    "mso-pagination:widow-orphan;" & _
                    "tab-stops:center 3.0in right 6.0in;" & _
                    "font-size:12.0pt;}")

    'Word uses the @page definition to store document layout settings for the entire document.
    'Using @page SectionN, Word applies page formatting to individual HTML elements referenced
    'through the class attribute.

    'mso-footer is the style attribute related to the footer 
    'Refer to the topic "Page Layout and Section Breaks" & "Headers and Footers" in the 

    'Office XML & HTML Reference for detailed info.

    strBody.Append("@page Section1" & _
                    "   {size:8.5in 11.0in; " & _
                    "   margin:1.0in 1.25in 1.0in 1.25in ; " & _
                    "   mso-header-margin:.5in; " & _
                    "   mso-footer: f1;" & _
                    "   mso-footer-margin:.5in; mso-paper-source:0;}" & _
                    " div.Section1" & _
                    "   {page:Section1;}" & _
                    "-->" & _
                    "</style></head>")

    strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" & _
                    "<div class=Section1>" & _
                    "<h1>A dynamically generated document with Footer</h1>" & _
                    "<p style='color:red'><I> This doc was generated on " & _
                    DateTime.Now & "</I></p><hr>")

    'We are building up a big string here so that the generated doc runs into multiple pages.

    For counter As Integer = 1 To 50
        strBody.Append("Lorem ipsum dolor sit amet, consectetur adipisicing elit, " & _
                        "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
    Next

    strBody.Append("</div>")

    'Word marks and stores information for simple fields by means of the Span element with the 
    'mso-field-code style. Refer to the topic "Fields" in the Office XML & HTML Reference

    strBody.Append("<div style='mso-element:footer' id=f1>" & _
                    " <p class=MsoFooter>" & _
                    " <span style='mso-tab-count:2'></span><span style='mso-field-code:"" PAGE ""'></span>" & _
                    " </p></div>" & _
                    "</body></html>")

    'Force this content to be downloaded 
    'as a Word document with the name of your choice

    Response.AppendHeader("Content-Type", "application/msword")
    Response.AppendHeader("Content-disposition", _
                            "attachment; filename=myword.doc")
    Response.Write(strBody)

End Sub

I am dynamically generating a word file, and clicking the link opens the file save dialog and it says the document is a: Microsoft Word 97 - 2003 Document.

What would I need to do to programmatically generate a 2007, 2010 word document.

Code behind:

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    'build the content for the dynamic Word document
    'in HTML alongwith some Office specific style properties. 

    Dim strBody As New System.Text.StringBuilder("")

    strBody.Append("<html " & _
                    "xmlns:o='urn:schemas-microsoft-com:office:office' " & _
                    "xmlns:w='urn:schemas-microsoft-com:office:word'" & _
                    "xmlns='http://www.w3.org/TR/REC-html40'>" & _
                    "<head><title></title>")

    'The setting specifies document's view after it is downloaded as Print Layout
    'instead of the default Web Layout. For the Header & Footer to be visible,
    'this mode is required.

    strBody.Append("<!--[if gte mso 9]>" & _
                    "<xml>" & _
                    "<w:WordDocument>" & _
                    "<w:View>Print</w:View>" & _
                    "<w:Zoom>90</w:Zoom>" & _
                    "<w:DoNotOptimizeForBrowser/>" & _
                    "</w:WordDocument>" & _
                    "</xml>" & _
                    "<![endif]-->")

    'we can tweak the MsoFooter class that is referenced by the footer, as required

    strBody.Append("<style>" & _
                    "<!-- /* Style Definitions */" & _
                    "p.MsoFooter, li.MsoFooter, div.MsoFooter" & _
                    "{margin:0in;" & _
                    "margin-bottom:.0001pt;" & _
                    "mso-pagination:widow-orphan;" & _
                    "tab-stops:center 3.0in right 6.0in;" & _
                    "font-size:12.0pt;}")

    'Word uses the @page definition to store document layout settings for the entire document.
    'Using @page SectionN, Word applies page formatting to individual HTML elements referenced
    'through the class attribute.

    'mso-footer is the style attribute related to the footer 
    'Refer to the topic "Page Layout and Section Breaks" & "Headers and Footers" in the 

    'Office XML & HTML Reference for detailed info.

    strBody.Append("@page Section1" & _
                    "   {size:8.5in 11.0in; " & _
                    "   margin:1.0in 1.25in 1.0in 1.25in ; " & _
                    "   mso-header-margin:.5in; " & _
                    "   mso-footer: f1;" & _
                    "   mso-footer-margin:.5in; mso-paper-source:0;}" & _
                    " div.Section1" & _
                    "   {page:Section1;}" & _
                    "-->" & _
                    "</style></head>")

    strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" & _
                    "<div class=Section1>" & _
                    "<h1>A dynamically generated document with Footer</h1>" & _
                    "<p style='color:red'><I> This doc was generated on " & _
                    DateTime.Now & "</I></p><hr>")

    'We are building up a big string here so that the generated doc runs into multiple pages.

    For counter As Integer = 1 To 50
        strBody.Append("Lorem ipsum dolor sit amet, consectetur adipisicing elit, " & _
                        "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
    Next

    strBody.Append("</div>")

    'Word marks and stores information for simple fields by means of the Span element with the 
    'mso-field-code style. Refer to the topic "Fields" in the Office XML & HTML Reference

    strBody.Append("<div style='mso-element:footer' id=f1>" & _
                    " <p class=MsoFooter>" & _
                    " <span style='mso-tab-count:2'></span><span style='mso-field-code:"" PAGE ""'></span>" & _
                    " </p></div>" & _
                    "</body></html>")

    'Force this content to be downloaded 
    'as a Word document with the name of your choice

    Response.AppendHeader("Content-Type", "application/msword")
    Response.AppendHeader("Content-disposition", _
                            "attachment; filename=myword.doc")
    Response.Write(strBody)

End Sub

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

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

发布评论

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

评论(1

那小子欠揍 2024-10-13 04:52:43

您的文件既不是 97 - 2003 年的文件,也不是 2007 - 2010 年的文件;这是一个 Word HTML 文档。

保存对话框显示 Word 97 - 2003 Document,因为您发送的扩展名不正确。
将扩展名(在 Content Disposition 标头中)更改为 .docx 将使保存对话框显示 Word Document,但不会使文档成为任何内容更有效。 (并且可能会使 Word 更有可能显示警告)

您需要生成 OpenXML 包,而不是 Word HTML。
您可以使用 Microsoft 的 OpenXML SDK 来执行此操作。
请注意,这将需要完全重写您的方法。

Your document is neither a 97 - 2003 nor a 2007 - 2010 document; it's a Word HTML document.

The save dialog says Word 97 - 2003 Document because you're sending an incorrect extension.
Changing the extension (in the Content Disposition header) to .docx will make the save dialog say Word Document, but will not make the document any more valid. (and will probably make Word more likely to show a warning)

You need to generate an OpenXML package, not Word HTML.
You can use Microsoft's OpenXML SDK to do this.
Note that this will require a complete rewrite of your method.

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