DataGrid导成Excel 的例子

发表于:2007-06-30来源:作者:点击数: 标签:
Response.Clear(); Response.Buffer= true; Response.ContentType = application/vnd.ms-excel; Response.Charset = ; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter o
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

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

DataGrid dg = new DataGrid();
dg.DataSource = GetDataSource(); //返回DataSet或DataTable
dg.DataBind();
dg.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();

如果导出有乱码:Response.ContentEncoding = System.Text.Encoding.UTF8


原文转自:http://www.ltesting.net