GridView导出问题,寻求更优方法
gridview格式:1.多行表头
2.带AspNetPager 分页
3.其中一列为超链接.
如果gridview中不带超链接时,我用如下方法可以导出成功。
public static string GridViewToExcel(GridView gv)
{
return GridViewToExcel(gv, null);
}
/// <summary>
/// GridView导出Excel文件
/// </summary>
/// <param name="gv">GridView</param>
/// <param name="file">文件名</param>
/// <returns></returns>
public static string GridViewToExcel(GridView gv, string file)
{
string fileName = "";
if (string.IsNullOrEmpty(file))
{
fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; // 文件名
}
else
{
fileName = file + ".xls";
}
fileName = fileName.Replace('(', '(').Replace(')', ')').Replace("/", "");//
Page page = new Page();
HtmlForm form = new HtmlForm();
gv.EnableViewState = false;
gv.AllowPaging = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
DisableControls(gv);
form.Controls.Add(gv);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");//
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.Charset = "utf-8";
//HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
// HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/excel";
StringBuilder sb = new StringBuilder();
System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
page.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
return fileName;
}
但是如果gridview中带超链接,则导出的数据不完整。
有没有那种方法可以实现导出后表头任然为多行,并且在又分页时数据导出完整? --------------------编程问答-------------------- string filename = "值班表" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
string style = @"<style> .text { mso-number-format:/@; } </script> "; //解决第一位字符为零时不显示的问题
string xlsHeader = @"<html xmlns:x=urn:schemas-microsoft-com:office:excel>";
xlsHeader += "<head>";
xlsHeader += "<!--[if gte mso 9]>";
xlsHeader += "<xml>";
xlsHeader += " <x:ExcelWorkbook>";
xlsHeader += " <x:ExcelWorksheets>";
xlsHeader += " <x:ExcelWorksheet>";
xlsHeader += " <x:Name>ExportData</x:Name>";
xlsHeader += " <x:WorksheetOptions>";
xlsHeader += " <x:Selected/>";
xlsHeader += " </x:WorksheetOptions>";
xlsHeader += " </x:ExcelWorksheet>";
xlsHeader += " </x:ExcelWorksheets>";
xlsHeader += " </x:ExcelWorkbook>";
xlsHeader += "</xml>";
xlsHeader += "<![endif]-->";
xlsHeader += "</head>";
filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);//解决导出EXCEL时乱码的问题
Response.ClearContent();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.Write(xlsHeader);
div_GV.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
this.DutyGrid.AllowPaging = true; --------------------编程问答-------------------- http://www.cnblogs.com/dreamof/archive/2008/06/13/1219437.html
这个方法很简单,不知道合不合你胃口
补充:.NET技术 , ASP.NET