2010-12-02 4 views
2

私は動的に単語ファイルを生成していますが、リンクをクリックするとファイル保存ダイアログが開き、文書は:Microsoft Word 97 - 2003文書です。ワードドキュメントのバージョンをプログラムで変更しますか?

2007年、2010年のワードドキュメントをプログラムで生成するには、何が必要ですか?背後

コード:

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 

答えて

3

あなたの文書が97もありません - 2003年も2007年から2010年のドキュメント。これはWordのHTML文書です。

不適切な拡張子を送信しているため、保存ダイアログにWord 97 - 2003 Documentと表示されます。
拡張子(Content Dispositionヘッダー)を.docxに変更すると、保存ダイアログにはWord Documentと表示されますが、その文書は有効になりません。 (おそらくWordに警告を表示する可能性が高くなります)

Word HTMLではなくOpenXMLパッケージを生成する必要があります。
MicrosoftのOpenXML SDKを使用してこれを行うことができます。
これには、メソッドの完全な書き換えが必要であることに注意してください。

関連する問題