将GridView中内容导入到Excel(或Word)中

导入Excel:

   Response.Clear();
    Response.Buffer 
= true;
    Response.Charset 
= "GB2312";
    Response.AppendHeader(
"Content-Disposition""attachment;filename=FileName.xls");
    
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
    Response.ContentEncoding = System.Text.Encoding.UTF7;
    Response.ContentType 
= "application/ms-excel";//设置输出文件类型为excel文件。 
    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:

   Response.Clear();
    Response.Buffer 
= true;
    Response.Charset 
= "GB2312";
    Response.AppendHeader(
"Content-Disposition""attachment;filename=FileName.xls");
    
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
    Response.ContentEncoding = System.Text.Encoding.UTF7;
    Response.ContentType 
= "application/ms-excel";//设置输出文件类型为excel文件。 
    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();

posted on 2007-01-19 10:58  醒了又睡的鱼.NET  阅读(616)  评论(2编辑  收藏  举报

导航