0

我正在使用 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 非常相似,但我如何将其转换为实际的。引导我。

4

1 回答 1

0

这对我有用,它需要itextsharp.dll

            string FName = "filename.doc";
            mygrid.AllowPaging = false;
            mygrid.DataSource = datasource();
            mygrid.DataBind();
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FName));
            Response.Charset = "";
            Response.ContentType = "application/ms-word";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            mygrid.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
于 2012-08-08T12:45:18.520 回答