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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/801255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:45:06  来源:igfitidea点击:

Problem writing HTML content to Word document in ASP.NET

c#asp.netms-wordexport

提问by

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

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

My Html display page is:

我的 Html 显示页面是:

  1. What is your favourite color?
  1. 你最喜欢的颜色是什么?

NA

不适用

  1. List the top three school ?
  1. 列出前三所学校?

one National two Devs three PS

一国民二开发三PS

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

还有一个用于点击事件的按钮。按钮点击事件将打开 MS word 并将页面内容粘贴到 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.

单词 page 包含 html 设计页面的 table 属性。它只出现在 Word 2003 中。但在 Word 2007 中,Word 文档包含没有表格属性的文本。如何在 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.

我正在通过 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();
}

回答by lstanczyk

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

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

回答by JasonPlutext

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

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

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.NETis one. Disclosure: I maintain that; you can find links to others elsewhere here on StackOverflow.

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

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

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

  • you have less control over how the import is performed;
  • AltChunk isn't supported by Word 2003 (even with the compatibility pack).
  • 您对导入的执行方式的控制较少;
  • Word 2003 不支持 AltChunk(即使有兼容包)。

回答by kmxr

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.

据我了解,您正在尝试即时创建 ms word 文档,并且在 Word 2003 与 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.

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

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".

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