问题描述
gridview格式:1.多行表头2.带AspNetPager分页3.其中一列为超链接.如果gridview中不带超链接时,我用如下方法可以导出成功。publicstaticstringGridViewToExcel(GridViewgv){returnGridViewToExcel(gv,null);}///<summary>///GridView导出Excel文件///</summary>///<paramname="gv">GridView</param>///<paramname="file">文件名</param>///<returns></returns>publicstaticstringGridViewToExcel(GridViewgv,stringfile){stringfileName="";if(string.IsNullOrEmpty(file)){fileName=DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";//文件名}else{fileName=file+".xls";}fileName=fileName.Replace('(','(').Replace(')',')').Replace("/","");//Pagepage=newPage();HtmlFormform=newHtmlForm();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";StringBuildersb=newStringBuilder();System.IO.StringWriterstringWrite=newSystem.IO.StringWriter(sb);System.Web.UI.HtmlTextWriterhtmlWrite=newSystem.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();returnfileName;}但是如果gridview中带超链接,则导出的数据不完整。有没有那种方法可以实现导出后表头任然为多行,并且在又分页时数据导出完整?
解决方案
解决方案二:
stringfilename="值班表"+DateTime.Now.ToString("yyyyMMdd")+".xls";stringstyle=@"<style>.text{mso-number-format:/@;}</script>";//解决第一位字符为零时不显示的问题stringxlsHeader=@"<htmlxmlns:x=urn:schemas-microsoft-com:office:excel>";xlsHeader+="<head>";xlsHeader+="<!--[ifgtemso9]>";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);StringWritersw=newStringWriter();HtmlTextWriterhtw=newHtmlTextWriter(sw);Response.Write(xlsHeader);div_GV.RenderControl(htw);Response.Write(style);Response.Write(sw.ToString());Response.End();this.DutyGrid.AllowPaging=true;
解决方案三:
这个方法很简单,不知道合不合你胃口