繁体   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