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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。