在HTML中将HTML内容写入Word文档时出现问题

我试图将HTML页面内容导出到Word。

我的Html显示页面是:

  1. 你最喜欢的颜色是什么?

NA

  1. 列出前三名的学校?

一个国家两个开发三PS

还有一个点击事件按钮。 按钮单击事件将打开MS word并将页面内容粘贴到word中。

单词页面包含html设计页面的表属性。 它仅在Word 2003中出现。但在word 2007中,word文档包含带有out属性的文本。 如何在word 2003中删除此表属性。

我无法添加快照。 否则我会告诉你。

我正在通过aspx设计网页。 我通过以下代码导出网页内容。

protected void Button1_Click(object sender, EventArgs e) { Response.ContentEncoding = System.Text.Encoding.UTF7; System.Text.StringBuilder SB = new System.Text.StringBuilder(); System.IO.StringWriter SW = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW); tbl.RenderControl(htmlTW); string strBody = "" + "" + "
" + htmlTW.InnerWriter.ToString() + "
" + "" + ""; Response.AppendHeader("Content-Type", "application/msword"); Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName); Response.ContentEncoding = System.Text.Encoding.UTF7; string fileName1 = "C://Temp/Excel" + DateTime.Now.Millisecond.ToString(); BinaryWriter writer = new BinaryWriter(File.Open(fileName1, FileMode.Create)); writer.Write(strBody); writer.Close(); FileStream fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read); byte[] renderedBytes; // Create a byte array of file stream length renderedBytes = new byte[fs.Length]; //Read block of bytes from stream into the byte array fs.Read(renderedBytes, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); FileInfo TheFile = new FileInfo(fileName1); if (TheFile.Exists) { File.Delete(fileName1); } Response.BinaryWrite(renderedBytes); Response.Flush(); Response.End(); }

您正在编写HTML,声称它的内容类型为“application / msword”,然后希望获得最佳效果。

有更多“正确”的方法来实现您的目标。

有一些项目可以将(X)HTML转换为WordML内容,其中docx4j-ImportXHTML.NET就是其中之一。 披露:我坚持认为; 您可以在StackOverflow上找到其他地方的链接。

或者,您可以使用Word的altChunk机制,但请注意:

  • 你对import的执行方式控制较少;
  • Word 2003不支持AltChunk(即使使用兼容包)。

您还可以尝试创建现在由MS Office识别的Open XML文档。 以下是代码示例的更多信息: http : //msdn.microsoft.com/en-us/library/bb656295.aspx

根据我的理解,您正在尝试动态创建一个ms word文档,并且在Word 2003与2007中查看输出时遇到困难。

在上面的代码中,您只是吐出html并强制它成为ms word文档。 我很惊讶它甚至有效。

相反,您可能希望使用Office Interop(Microsoft.Office.Interop.Word)或使用nuget安装DocX。 在线查看一些示例,搜索“C#create word doc”。