簡體   English   中英

將HTML編輯器內容保存為C#中的內容類型.DOCX

[英]Save HTML EDITOR CONTENT to Content-type .DOCX in C#

 private void ConvertHTMLtoDOCX(string txtcode)
 {
     System.Text.StringBuilder strBody = 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>Time</title>");

     //The setting specifies document's view after it is downloaded as Print
     //instead of the default Web Layout
     strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");


     strBody.Append("<style>" + "<!-- /* Style Definitions */" + "@page Section1" + "   {size:8.5in 11.0in; " + "   margin:1.0in 1.25in 1.0in 1.25in ; " + "   mso-header-margin:.5in; " + "   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>" + Html_editor.Content + "</div></body></html>");

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


     string FullFilePath = @"C:\Users\ravikant\Desktop\AR GitHub\07-05-2014\FinalTestARGithub\LetterTemplate\"+ txtcode+ ".docx"; 

     FileInfo file = new FileInfo(FullFilePath);
     if (file.Exists)
     {
        ClientScript.RegisterStartupScript(this.GetType(), "disExp", "<script>alert('File Already Exists');</script>");
     }
     else
     {
         Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         Response.AppendHeader("Content-disposition", "inline; filename="+txtcode+".docx");
         Response.Write(strBody);
     }       
 }

這是使用CONTENT-TYPE的.DOCX代碼“ application / vnd.openxmlformats-officedocument.wordprocessingml.document”,打開文件時內容損壞。

嘗試執行此操作以了解文件的狀態。

我已經使用本地單詞.docx完成了此操作,但是沒有以這種方式生成.docx,因此它可能會起作用,也可能不會起作用。

  1. 復制保存的文件,將其范圍從.docx更改為.zip。
  2. 嘗試打開它。 我們正在嘗試找到文件document.xml,該文件通常在“ word”文件夾中。
  3. 在文本編輯器中打開它,查看是否有任何錯誤跳出或嘗試通過XML驗證器進行處理。 VisualStudio應該足以顯示任何格式錯誤。

在線XML驗證程序可能會有所幫助: http : //www.xmlvalidation.com/

以下幾行也是可疑的:

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

由於我不確定word將如何處理IE條件注釋。 注釋掉或刪除此行,看看會發生什么。

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

由於嵌套注釋。 <!-- /* */--> 也許嘗試將其更改為: strBody.Append("</head>"); 看看是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM