码迷,mamicode.com
首页 > Web开发 > 详细

gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)

时间:2016-09-19 16:10:36      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

 

调用 ExportGridViewToExcel(dt, HttpContext.Current.Response);

private void ExportGridViewToExcel(DataTable tb, HttpResponse response)
{

response.Clear();
response.Charset = "";
response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
response.ContentEncoding = System.Text.Encoding.UTF8;//System.Text.Encoding.GetEncoding("GB2312");
response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

GridView newDataGrid = new GridView();
newDataGrid.EnableViewState = false;
newDataGrid.AllowSorting = false;
newDataGrid.GridLines = GridLines.Vertical;
newDataGrid.HeaderStyle.Font.Bold = true;

newDataGrid.DataSource = tb.DefaultView;
newDataGrid.DataBind();
newDataGrid.RenderControl(htmlTextWriter);
response.Write(stringWriter.ToString());
}

gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)

标签:

原文地址:http://www.cnblogs.com/buy0769/p/5885288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!