首页 > 其他 > 详细

dataset导出成excel

时间:2015-06-03 15:30:18      阅读:301      评论:0      收藏:0      [点我收藏+]

之前网上查找了很多关于这类的代码。要不是中文乱码,要不是就是太复杂。这个是我用过最好用的。

//ds为数据源,filename为保存的文件名
publicvoidCreateExcel(DataSet ds,stringFileName)
{
//设置信息
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Charset="GB2312";
//保存的文件名
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+HttpUtility.UrlEncode(FileName));
//保存excel的文件流
StringWriter stringWrite =newStringWriter();
HtmlTextWriter htmlWrite =newHtmlTextWriter(stringWrite);
//数据的ds
DataGrid dg =newDataGrid();
dg.DataSource= ds;
dg.DataBind();
dg.RenderControl(htmlWrite);
//输出数据
HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>"+ stringWrite.ToString());
HttpContext.Current.Response.End();
}

 

dataset导出成excel

原文:http://www.cnblogs.com/xiaoqiang-X/p/4549039.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!