private void outprint()
{
Response.Clear();
          Response.Buffer= true;
          Response.Charset="GB2312";

System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter txtwriter = new HtmlTextWriter(writer);
//将DataGrid中的内容输出到txtwriter流中
this.DataGrid1.RenderControl(txtwriter); //作为附件输出,filename=ReqExcel.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc|.xls|.txt|.htm  
Response.ContentType = "application/ms-excel"; //ContentType指定文件类型 可以为application/ms-excel    
//application/ms-word | application/ms-txt | application/ms-html 或其他浏览器可直接支持文档   Response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls"); //下载
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");   
Response.Write(writer); Response.End();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

} private void Button1_Click(object sender, System.EventArgs e)
{
this.outprint();
}
}

解决方案 »

  1.   

    前面的代码稍微调整一下:
    System.IO.StringWriter writer = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter txtwriter = new HtmlTextWriter(writer);
    //将DataGrid中的内容输出到txtwriter流中
    this.DataGrid1.RenderControl(txtwriter);//作为附件输出,filename=ReqExcel.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc|.xls|.txt|.htm  Response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls"); //下载
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF7");  Response.ContentType = "application/ms-excel"; //ContentType指定文件类型 可以为application/ms-excel    
    //application/ms-word | application/ms-txt | application/ms-html 或其他浏览器可直接支持文档  Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF7");
     
    Response.Write(writer.ToString());Response.End();
      

  2.   

    删一句Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF7"); 
    代码太多了,编辑的时候没看到,重复了,汗