5

我正在尝试将 HTML 页面内容导出到 Word。

我的 Html 显示页面是:

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

不适用

  1. 列出前三名的学校?

一国二开发三PS

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

word page包含html设计页面的table属性。它只出现在 Word 2003 中。但在 Word 2007 中,word 文档包含没有表属性的文本。如何在 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 = "<html>" +
        "<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
        "</body>" +
        "</html>";

    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();
}
4

4 回答 4

1

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

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

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

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

  • 您对如何执行导入的控制较少;
  • Word 2003 不支持 AltChunk(即使有兼容包)。
于 2014-11-17T23:26:37.100 回答
0

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

于 2009-06-30T15:56:06.187 回答
0

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

在上面的代码中,您只是简单地吐出 html 并强制它成为 ms word 文档。我很惊讶它甚至可以工作。

相反,您可能想要使用 Office Interop (Microsoft.Office.Interop.Word) 或使用 nuget 安装 DocX。在网上看一些例子,搜索“C# create word doc”。

于 2014-11-18T06:25:43.407 回答
0

问题不清楚。

好吧,这些链接可以帮助您理解 MSWord-C# 自动化:

http://www.codeproject.com/KB/cs/Simple_Ms_Word_Automation.aspx

http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

于 2009-04-29T08:07:25.933 回答