【问题标题】:Save HTML EDITOR CONTENT to Content-type .DOCX in C#在 C# 中将 HTML EDITOR CONTENT 保存为 Content-type .DOCX
【发布时间】:2023-03-26 23:32:01
【问题描述】:
 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);
     }       
 }

这是使用 .DOCX “application/vnd.openxmlformats-officedocument.wordprocessingml.document”的 CONTENT-TYPE 的代码,打开文件时内容已损坏。

【问题讨论】:

  • 检查文件的文件大小。看起来是否有任何实际保存到文件中的内容?另外我认为您的file.Exists 支票是错误的。这将检查文件是否存在于服务器上,当您保存到客户端时。在您的开发机器上可能是相同的,但是一旦您发布到远程服务器,您就会遇到问题。
  • 文件大小200kb还好,说明内容有问题。不知道为什么会发生这种情况,当我制作 .doc 或 .xlsx 时,这很容易。编译代码也没有报错,请关注此事。
  • 第一步,我会从您保存的内容中删除编辑器内容。在其位置放置一个基本的&lt;p&gt;Hello World&lt;/p&gt;。如果可行,您就知道问题出在编辑器的内容上。检查 html、xml、doctype、head 和 body 标记的内容。如果它不起作用,则说明您的保存过程有问题。
  • file.exists 工作正常,从那里没有问题。我也以不同的格式创建文件,文件很容易创建,但对于 .DOCX 我不知道。
  • 另一个可能有帮助的技巧,我已经使用原生单词 .docx 完成了此操作,但不是以这种方式生成的 .docx,因此它可能有效,也可能无效。制作保存文件的副本,将其扩展名从 .docx 更改为 .zip。尝试打开它。我们试图找到一个文件document.xml,它通常位于“word”文件夹中。在文本编辑器中打开它,看看是否有任何错误或尝试通过 XML 验证器。 VisualStudio 应该足以显示任何格式错误。

标签: c# asp.net .net


【解决方案1】:

试试这个来找出文件发生了什么。

我已经使用原生单词 .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 条件 cmets。注释掉或删除这一行,看看会发生什么。

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>");

由于嵌套的 cmets。 &lt;!-- /* */--&gt;。也许尝试将其更改为:strBody.Append("&lt;/head&gt;"); 看看是否可行。

【讨论】:

  • 你的方法很好,但还是有问题。先生,我知道这里不允许这种类型的 pf 语言,但我很紧急,你能帮帮我吗?请给出一些将 html 编辑器内容保存为 .docx 格式的代码。
  • 抱歉,如果没有更多信息,我无能为力。我从来没有从 HTML 编辑器保存到 .docx 文件。我在这里一直试图做的是缩小问题的范围。通过我的回答和以前的 cmets,我一直在尝试确定是来自编辑器的文本导致问题还是您用来保存问题的方法。更具体地说,是标题等,以便您将内容包装在其中。分解问题。尝试先保存一些硬编码的 HTML。看看这是否有效,如果有效,那么您将文本包裹在其中的内容是可以的,您需要查看文本中的内容...
  • ...编辑器。它可能包含使 ​​XML 无效的标签。如果使用一些硬编码文本保存不起作用,那么您在包装文本编辑器的内容时​​使用的附加 XML 标记存在问题。
  • 好的,先生,我将 HardText 放到我的代码中,但出现了相同的 prblm。我使用 2010 年的办公室是否会因此而发生任何问题。 ' strBody.Append("" + "

    Hello World

猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 2014-11-17
  • 2011-01-23
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
相关资源
最近更新 更多