首页 > 代码库 > asp.net导出excel较为简约的代码

asp.net导出excel较为简约的代码

ds为数据源

 

Response.Buffer = true;            

Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);       //文件名    

Response.ContentType = "application/ms-excel";            

StringWriter stringWrite = new StringWriter();            

HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

DataGrid dg = new DataGrid();            

dg.DataSource = ds;            

dg.DataBind();            

dg.RenderControl(htmlWrite);            

Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");     //处理乱码       

Response.Write(stringWrite.ToString());             //写缓冲区中的数据到HTTP头文件中            

Response.End();

asp.net导出excel较为简约的代码