问题描述
asp.net2.0中导出EXCEL时内容为什么始终为空?实现方式,首先生成HTML摸板[格式如下,内容太长删除了一些]privateStringBuilderpHead=newStringBuilder();privateStringBuilderpBody=newStringBuilder();privateStringBuilderpFoot=newStringBuilder();publicRptSpareQuery(){pHead.Append("<htmlxmlns:v='urn:schemas-microsoft-com:vml'");pHead.Append("xmlns:o='urn:schemas-microsoft-com:office:office'");pHead.Append("<o:Created>1996-12-17T01:32:42Z</o:Created>");pHead.Append("<o:LastSaved>2008-04-23T06:46:22Z</o:LastSaved>");pHead.Append("<trclass=xl26height=20style='mso-height-source:userset;height:15.0pt'>");pHead.Append("<tdheight=20class=xl29style='height:15.0pt;border-left:none'></td>");pHead.Append("</tr>");pHead.Append("<trclass=xl26height=20style='mso-height-source:userset;height:15.0pt'>");pHead.Append("<tdheight=20class=xl34style='height:15.0pt;border-top:none;border-left:");pHead.Append("none'>制单<ruby>日<spanstyle='display:none'><rtclass=font6style='mso-char-type:");pHead.Append("katakana'>ヒ</rt></span></ruby>期:</td>");pHead.Append("<tdclass=xl42style='border-top:none;border-left:none'x:num>20080526</td>");pHead.Append("<tdclass=xl34style='border-top:none;border-left:none'>制单<ruby>人<span");pHead.Append("style='display:none'><rtclass=font6style='mso-char-type:katakana'>シャ</rt></span></ruby>:</td>");pHead.Append("<tdclass=xl31style='border-top:none;border-left:none'>KimLuo</td>");pHead.Append("<tdclass=xl30style='border-left:none'> </td>");pHead.Append("</tr>");pHead.Append("<trheight=20style='height:15.0pt'>");pHead.Append("<tdheight=20colspan=6style='height:15.0pt;mso-ignore:colspan'></td>");pHead.Append("</tr>");pHead.Append("<trheight=19style='height:14.25pt'>");pHead.Append("<tdheight=19class=xl27style='height:14.25pt'>询价番号</td>");pHead.Append("<tdclass=xl27style='border-left:none'>品名</td>");pHead.Append("<tdclass=xl27style='border-left:none'>规格</td>");pHead.Append("<tdclass=xl27style='border-left:none'>制造商</td>");pHead.Append("<tdclass=xl27style='border-left:none'>数量</td>");pHead.Append("<tdclass=xl27style='border-left:none'>单位</td>");pHead.Append("</tr>");//pBodypBody.Append("<trheight=19style='height:14.25pt'>");pBody.Append("<tdheight=19class=xl32align=rightstyle='height:14.25pt;border-top:none'x:num>#SpareQueryNo#</td>");pBody.Append("<tdclass=xl32style='border-top:none;border-left:none'>#PartName#</td>");pBody.Append("<tdclass=xl32style='border-top:none;border-left:none'>#Speccification#</td>");pBody.Append("<tdclass=xl32style='border-top:none;border-left:none'>#Maker#</td>");pBody.Append("<tdclass=xl32align=rightstyle='border-top:none;border-left:none'x:num>#Quantity#</td>");pBody.Append("<tdclass=xl32style='border-top:none;border-left:none'>#Unit#</td>");pBody.Append("</tr>");//PFootpFoot.Append("<![ifsupportMisalignedColumns]>");pFoot.Append("<trheight=0style='display:none'>");pFoot.Append("<tdwidth=125style='width:94pt'></td>");pFoot.Append("<tdwidth=125style='width:94pt'></td>");pFoot.Append("<tdwidth=125style='width:94pt'></td>");pFoot.Append("<tdwidth=85style='width:64pt'></td>");pFoot.Append("<tdwidth=85style='width:64pt'></td>");pFoot.Append("<tdwidth=103style='width:77pt'></td>");pFoot.Append("</tr>");pFoot.Append("<![endif]>");pFoot.Append("</table>");pFoot.Append("</body>");pFoot.Append("</html>");}publicstringHead{get{returnpHead.ToString();}}publicstringBody{get{returnpBody.ToString();}}publicstringFoot{get{returnpFoot.ToString();}}然后写了一个方法:///<summary>///导出Excel文件///</summary>///<paramname="excelFile">head&body&foot</param>publicvoidExportExcel(stringexcelFile){try{StringBuildersb=newStringBuilder();sb.Append(excelFile);System.IO.StringWriteroStringWriter=newSystem.IO.StringWriter(sb);Response.ClearHeaders();Response.ClearContent();Response.Clear();Response.Buffer=true;Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");Response.ContentType="application/vnd.ms-excel";Response.Charset="utf-8";StringrNumber=(DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()).ToString();Response.AppendHeader("Content-Disposition","attachment;filename=Jyobihin"+rNumber+".xls");this.EnableViewState=false;Response.Write(oStringWriter.ToString());Response.End(); }catch(TTNIExceptionex){ApplicationLog.WriteError(ex.Message);}}请问为什么用这种方式导出EXCEL时里面没有内容?在VS2003下面这种方式又可以.麻烦给个解决方案:)
解决方案
解决方案二:
你看下生成的源码有什么不同
解决方案三:
Response.Clear();Response.Charset="gb2312";Response.AddHeader("content-disposition","attachment;filename=shengchanpaicheng.xls");Response.ContentEncoding=System.Text.Encoding.UTF7;Response.ContentType="application/ms-excel";System.IO.StringWriterstrw=newSystem.IO.StringWriter();System.Web.UI.HtmlTextWriterhtmw=newHtmlTextWriter(strw);GridView1.AllowPaging=false;GridView1.DataSource=bindgrid();GridView1.DataBind();GridView1.RenderControl(htmw);Response.Write(strw.ToString());Response.End();GridView1.AllowPaging=true;bindgrid();
解决方案四:
excelFile的内容有么?
解决方案五:
单步调试一下.看你的excelFile有内容吗
解决方案六:
up看看参数excelFile是不是空的
解决方案七:
excelFile有内容,我将生成的内容保存为html格式可以正确显示出来,但是输出到EXCEL时却为空白.
解决方案八:
帮顶
解决方案九:
这个是我的导出代码,有的是调用的,不过应该是有启发的哦stringsql="";stringtname="";GetDataLaydata=newGetDataLay(Page);stringdt=System.DateTime.Today.ToString("yyyyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo);Configcon=newConfig(Page);sql="selectID,ename,fname,regfund,addr,area,jydes,conduct,carea,gsreg,swreg,gsdes,pid+','aspid,pnum,telfromv_enterwhere";tname=con.GetFieldValue("des","v_departwhereid='"+Page.Session["cid"]+"'");if(lsdCID.SelectedIndex>=0){sql+="CIDlike'"+lsdCID.SelectedValue+"%'";}if(ctrBegin.Text!=""){sql+="andRDate>='"+ctrBegin.Text+"'";}if(ctrEnd.Text!=""){sql+="andRDate<'"+ctrEnd.Text+"23:59:59.999'";}System.Data.SqlClient.SqlDataReaderdr=data.GetDataReader(sql);if(dr!=null){if(dr.HasRows){Response.Clear();Response.Buffer=true;Response.Charset="GB2312";Response.AppendHeader("Content-Disposition","attachment;filename=Fjjs"+dt+".xls");Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文Response.ContentType="application/ms-excel";//设置输出文件类型为excel文件。this.EnableViewState=false;Response.Write("tttttt废旧金属收购站点统计表tn");Response.Write("填报单位:"+tname+"ttttttttttttt"+System.DateTime.Now.ToString("D")+"tn");Response.Write("序号t企业名称t法人代表t注册资金t经营地址t营业面积t经营类型t经营品种t仓库面积t工商登记注册号t税务登记号t注册类型t法人身份证号t从业人员t类型电话tn");sql="";while(dr.Read()){sql="";for(inti=0;i<dr.FieldCount;i++){sql+="t"+dr[i].ToString();}Response.Write(sql.Substring(1)+"n");}sql=null;dr.Close();data.Close();Response.End();}else{dr.Close();data.Close();sql="没有您选择的汇总表!";}}else{dr.Close();data.Close();sql="形成汇总表出错,请稍后重试。";}if(sql!=null){Page.Response.Write(sql);}
解决方案十:
单步根据,内容有没有输出到客户端,如有,看看内容,将它另存为.xls文件,用excel打开.