asp.net导出把网页导入word(转载)

protected void btnPdf_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";

        //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开

        //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm

        Response.AppendHeader("Content-Disposition", "attachment;filename=table.doc");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        //Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档

        Response.ContentType = "application/ms-word";
        this.EnableViewState = false;


        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        this.detailContent.RenderControl(oHtmlTextWriter);

        //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件

        Response.Write(oStringWriter.ToString());
        Response.End();
    }

 

问题

(1)出现只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示在此页面的.aspx页面头中要加EnableEventValidation = "false"

(2):Response.AppendHeader("Content-Disposition", "attachment;filename=table.doc");table.doc该为中文是,另存为对话框上面的文件名成乱码。

(3)有弹出的保存对话框,怎样才能作出 点击一个按钮就自动保存到设置的路径???

posted on 2010-07-07 17:03  酒囊饭袋  阅读(1325)  评论(1)    收藏  举报

导航