C# ASP.NET GridView导出EXCEL。
2014-09-10 16:15
501 查看
C# CODE:
using System.IO;
using System.Web.UI;
public void GridViewToExcel(Control ctrl)
{
try
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode("CurrentTaskStates.xls", System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
catch
{
//log.WriteLog(ex);
// return;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}///重载,这个一定要有。
using System.IO;
using System.Web.UI;
public void GridViewToExcel(Control ctrl)
{
try
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode("CurrentTaskStates.xls", System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
catch
{
//log.WriteLog(ex);
// return;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}///重载,这个一定要有。
相关文章推荐
- Datatable或Gridview中的数据导出为excel表格数据(Asp.net C#)
- C# ASP.net 将GridView中的数据导出到Excel中的代码
- C# asp.net 把GridView数据 导出到 Excel
- asp.net c# repeater或gridview导出EXCEL的详细代码。
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- [ASP.NET-C#]将DataSet、DataTable中的数据导出为Excel的解决方法
- [导入]ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- [译]ASP.Net 2.0: Export GridView to Excel (转) 如果GridView中有其它控件,比如Checkboxes,Dropdownlists,我们需要将它转换为其相关的值,以下递归就用于导出Excel前的准备工作,将各类控件转换为其相关值.
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)