大众计算机学习网

专门提供各种电脑教程

将Panel控件的数据导出到WROD并下载的代码

Admin | 2015-5-12 10:49:26 | 被阅次数 | 1905

  将Panel控件的数据,先导入到Word之后,再下载Word的功能代码如下,提供两种代码。

  第一种代码

  StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        this.Panel1.RenderControl(hw);

        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.Clear();
        response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        response.ContentType = "application/vnd.ms-word";

        response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlPathEncode(Server.UrlDecode("文档名称") + ".doc"));
        //response.Charset = "utf-8";
        response.Write(tw.ToString());
        response.End();

  第二种代码

  Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=文档名称.doc");
        //Response.ContentType = "application/word";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.ContentType = "application/word";
        Response.ContentType = "application/vnd.ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        Panel1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

  上面的代码只是能实现导入word下载的功能,但是,假如控件中有图片或有网页换行等标记,不能在word中自动转换。也就是说,只能将控件中的网页源代码内容导入到WORD中,不能实现相关标记的自动识别替换。