1. 程式人生 > >asp.net web頁面匯出word的一種方式,自己記錄用

asp.net web頁面匯出word的一種方式,自己記錄用

前臺:div內可新增內容

<div id="divText" runat="server" style="text-align: center">
</div>

後臺:利用RenderControl()將資料寫入輸出流中,然後用word的格式輸出到客戶端

        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(lbTitle.Text + ".doc", Encoding.UTF8).ToString());
        Response.ContentType = "application/ms-word";
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        divText.RenderControl(hw);
        Response.Write(tw.ToString().Replace("border=\"0\"", "border=\"1\""));
        Response.End();