最近做的项目客户要求将DataGrid中的数据导出到Excel中,于是我这几天一直在找比较好的解决方案,不是内容不全就是找不到我理想的生成Excel方法(由于项目需要导出到Excel时,保存该Excel文件需要中文名。)。今天终于有所收获,希望我的这个能对大家解决类似的问题有所帮助。
写了一个导出到Excel的类:
/// <summary>从DataGrid导出指定类型的文件</summary>
/// <summary>Super 2005/8/18</summary>
/// <param name="ctl">DataGrid的ID</param>
/// <param name="FileType">导出文件类型//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel</param>
/// <param name="FileName">导出的文件名</param>
/// <returns>文件</returns>
public static void ExportDataGrid(System.Web.UI.Control ctl,string FileType, string FileName)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+FileName);
HttpContext.Current.Response.Charset ="gb2312";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType
=FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
只要调用导出到Excel类就ok了
private void btnExportToExcel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
if( grid.Items.Count == 0 )
{
throw new Exception("没有数据不能导出!");
}
string FileName="中文报表";
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)+".xls";
Common.ExportDataGrid(grid,"application/ms-excel",FileName);
}
catch(Exception ex)
{
Response.Write("<script language =javascript >alert('"+ex.Message+"');</script>");
return;
}
}
这只是简单的导出到Excel中解决方法,应该还有更多更好的方法吧,希望能在以后的学习工作中不断地总结。
写了一个导出到Excel的类:
/// <summary>从DataGrid导出指定类型的文件</summary>
/// <summary>Super 2005/8/18</summary>
/// <param name="ctl">DataGrid的ID</param>
/// <param name="FileType">导出文件类型//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel</param>
/// <param name="FileName">导出的文件名</param>
/// <returns>文件</returns>
public static void ExportDataGrid(System.Web.UI.Control ctl,string FileType, string FileName)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+FileName);
HttpContext.Current.Response.Charset ="gb2312";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType
=FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
只要调用导出到Excel类就ok了
private void btnExportToExcel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
if( grid.Items.Count == 0 )
{
throw new Exception("没有数据不能导出!");
}
string FileName="中文报表";
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)+".xls";
Common.ExportDataGrid(grid,"application/ms-excel",FileName);
}
catch(Exception ex)
{
Response.Write("<script language =javascript >alert('"+ex.Message+"');</script>");
return;
}
}
这只是简单的导出到Excel中解决方法,应该还有更多更好的方法吧,希望能在以后的学习工作中不断地总结。