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

发布于 2024-07-18 21:15:26 字数 1989 浏览 1 评论 0原文

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

我的Html显示页面是:

  1. 你最喜欢什么颜色?

NA

  1. 列出前三名学校?

一名国家级 两个开发人员 三个PS

和一个用于点击事件的按钮。 按钮单击事件将打开 MS Word 并将页面内容粘贴到 Word 中。

word页面包含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();
}

I am trying to export the HTML page contents to Word.

My Html display page is:

  1. What is your favourite color?

NA

  1. List the top three school ?

one National
two Devs
three PS

And a button for click event. The button click event will open MS word and paste the page contents in word.

The word page contains the table property of html design page. It occurs only in Word 2003. But in word 2007 the word document contains the text with out table property. How can I remove this table property in word 2003.

I am not able to add the snapshots. Else i will make you clear.

I am designing the web page by aspx. I am exporting the web page content by the following code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提笔书几行 2024-07-25 21:15:26

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

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

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

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

  • 您对导入执行方式的控制较少;
  • Word 2003 不支持 AltChunk(即使有兼容包)。

You are writing HTML, claiming it is of content type "application/msword", then hoping for the best..

There are more "correct" ways to achieve your objective.

There are a few projects around for converting (X)HTML to WordML content, of which docx4j-ImportXHTML.NET is one. Disclosure: I maintain that; you can find links to others elsewhere here on StackOverflow.

Alternatively, you can use Word's altChunk mechanism, though note:

  • you have less control over how the import is performed;
  • AltChunk isn't supported by Word 2003 (even with the compatibility pack).
﹎☆浅夏丿初晴 2024-07-25 21:15:26

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

You can also try to create an Open XML document that is now recognized by MS office.
Here is some more info with code samples:
http://msdn.microsoft.com/en-us/library/bb656295.aspx

自演自醉 2024-07-25 21:15:26

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

在上面的代码中,您只是简单地吐出 html 并强制它成为MS Word文档。 我很惊讶它居然还能起作用。

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

From what I understand, you are trying to create a ms word document on the fly and are having difficulty when the output is viewed in Word 2003 vs. 2007.

In your code above, you are simply spitting out html and forcing it to be a ms word document. I'm surprised it even works.

Instead, you might want to use Office Interop (Microsoft.Office.Interop.Word) or install DocX using nuget. Look at some examples online, search for "C# create word doc".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文