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();

posted on 2015-01-23 16:50  changyehappy  阅读(176)  评论(0编辑  收藏  举报

导航