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

アプリケーションは問題なく実行されていますが、次のような単語でデータを返します

   +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 に答える