【发布时间】:2012-06-18 19:51:49
【问题描述】:
我正在使用 c# 进行 asp.net 4.0 Web 应用程序开发。我正在尝试将 Gridview 数据导出到 ms word 文档。我使用了以下代码。
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=MyWord.doc");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/vnd.word";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
应用程序运行没有问题,但它以 word 形式返回数据
+ADw-div+AD4- +ADw-table cellspacing+AD0AIg-0+ACI- cellpadding+AD0AIg-4+ACI- id+AD0AIg-GridView1+ACI- style+AD0AIg-color:+ACM-333333+ADs-border-collapse:collapse+ADsAIgA+- +ADw-tr style+AD0AIg-color:White+ADs-background-color:+ACM-507CD1+ADs-font-weight:bold+ADsAIgA+- +ADw-th scope+AD0AIg-col+ACIAPg-FileId+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-PortalId+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-FileName+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Extension+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Size+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Width+ADw-
我知道它与 html 非常相似,但我如何将其转换为实际的。指导我。
【问题讨论】:
-
我不知道您在 HTML 和您发布的 sn-p 之间看到了什么相似之处,但我知道
.docMS Word 格式是专有的二进制文件格式吗?你有没有在记事本中打开过由 MS Word 生成的.doc文件,你看到它长什么样了吗?您似乎正在编写一些显然与实际格式无关的文本文件。 -
这里我在文档中找到了 div,style,cellpadding,table 和 td,th。我们如何将二进制格式转换为实际的表结构
-
您必须使用第三方库。 .NET 没有任何内置功能可让您生成 Word 文件。
-
最新版本的 MS Word 使用开放标准,本质上是一个 xml 文档。确保您符合此标准并在其上添加 .docx 扩展名。 Start here 了解一下。