当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

关于Asp.net中使用以下代码导出Excel表格的问题

  private void ExportExcelFromDataGrid( string filename , System.Web.UI.WebControls.DataGrid ToExcelGrid )

  {  Response.Clear();

  Response.Buffer=   true;

  Response.Charset="utf-8";

  Response.AppendHeader("Content-Disposition","attachment;filename="+Server.UrlEncode ( filename ) );

  Response.ContentEncoding=System.Text.Encoding.Default;//设置默認输出流

  Response.ContentType   =   "application/ms-excel";//设置输出文件类型为excel文件。

  this.EnableViewState   =   false;

  System.Globalization.CultureInfo   myCItrad   =   new   System.Globalization.CultureInfo("ZH-CN",true);

  System.IO.StringWriter   oStringWriter   =   new   System.IO.StringWriter(myCItrad);

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

  ToExcelGrid.RenderControl(oHtmlTextWriter);

  Response.Write(oStringWriter.ToString());

  Response.End();

  }

  private void Button4_Click(object sender, System.EventArgs e)

  {

  this.Panel1.Visible = false;

  string filename = "内训师.xls";

  this.DataGrid1.Columns[6].Visible = true;

  this.DataGrid1.Columns[7].Visible = true;

  this.DataGrid1.Columns[this.DataGrid1.Columns.Count-1].Visible = false;

  this.DataGrid1.Columns[this.DataGrid1.Columns.Count-2].Visible = false;

  this.DataGrid1.AllowSorting = false;

  this.DataGrid1.AllowPaging = false;

  this.DataGrid1.SelectedIndex = -1;

  this.BindGrid();

  this.ExportExcelFromDataGrid ( filename , this.DataGrid1 );

  }

相关内容
赞助商链接