问题描述
我在将Ext.Net的GridPanel控件导出Excel文件时导出的Excel不认列,将一整行的数据都放在了一个单元格里面。方法是使用的Ext官方的例子。varsaveData=function(){GridData.setValue(Ext.encode(gvV_Wms_LocStock.getStore().reader.jsonData));}
前台页面<formid="form1"runat="server"><divrunat="server"id="div1"><ext:ResourceManagerID="resManager"runat="server"></ext:ResourceManager><ext:HiddenID="GridData"runat="server"/><ext:StoreID="store1"runat="server"OnRefreshData="str_RefreshData"AutoLoad="true"RemotePaging="true"RemoteSort="true"ShowWarningOnFailure="false"><Reader></Reader><Proxy><ext:PageProxy></ext:PageProxy></Proxy><SortInfoField="ID"Direction="DESC"/></ext:Store><ext:ViewportID="Viewport2"runat="server"Layout="FitLayout"><Items><ext:PanelHeader="false"ID="panQury"runat="server"Layout="FitLayout"AutoScroll="false"Border="false"><TopBar><ext:ToolbarID="toolMenu"runat="server"><Items><ext:ButtonID="btn_Print"Text="打印标签"Hidden="false"AutoPostBack="false"runat="server"Icon="Printer"><Listeners><ClickHandler="btn_Print_Click();"/></Listeners></ext:Button><ext:ButtonID="btn_Export"Text="导出"runat="server"IconCls="PageExcel"AutoPostBack="true"OnClick="ToExcel"><Listeners><ClickFn="saveData"/></Listeners></ext:Button><ext:ButtonID="btn_LS"Text="流水账"Hidden="false"AutoPostBack="false"runat="server"Icon="Find"><Listeners><ClickHandler="btn_Import_LS();"/></Listeners></ext:Button><ext:ButtonID="btn_TZ"Text="收发存明细表"Hidden="false"AutoPostBack="false"runat="server"Icon="Find"><Listeners><ClickHandler="btn_Import_TZ();"/></Listeners></ext:Button><ext:ButtonID="btn_HZ"Text="收发存汇总表"Hidden="false"AutoPostBack="false"runat="server"Icon="Find"><Listeners><ClickHandler="btn_Import_HZ();"/></Listeners></ext:Button><ext:ButtonID="btn_JD"Text="基地汇总表"Hidden="false"AutoPostBack="false"runat="server"Icon="Find"><Listeners><ClickHandler="btn_Import_JD();"/></Listeners></ext:Button></Items></ext:Toolbar></TopBar><Items><ext:GridPanelLayout="Fit"Header="false"Title="库位库存"Border="false"ID="gvV_Wms_LocStock"AutoWidth="true"TrackMouseOver="true"AutoExpandColumn="_Index"runat="server"StripeRows="true"StoreID="store1"><TopBar><ext:ToolbarCls="toolbar"><Content><uc1:QueryContrID="QueryContr"runat="server"/></Content></ext:Toolbar></TopBar><ColumnModel><Columns><%--<ext:ColumnHeader="Change"Width="75"DataIndex="change"/><ext:ColumnHeader="Pct.Change"Width="75"DataIndex="pctChange"/><ext:DateColumnHeader="LastUpdated"Width="85"DataIndex="lastChange"/>--%></Columns><Listeners></Listeners></ColumnModel><View><ext:LockingGridViewID="LockingGridView2"runat="server"/></View><DirectEvents></DirectEvents><SelectionModel><ext:RowSelectionModelID="RowSelectionModel1"runat="server"SingleSelect="false"><Listeners></Listeners></ext:RowSelectionModel></SelectionModel><Listeners></Listeners><LoadMaskShowMask="true"Msg="载入中..."/><SaveMaskShowMask="true"Msg="保存中..."/><BottomBar><ext:PagingToolbarID="pageTool"runat="server"PageSize="20"StoreID="store1"RefreshText="刷新"FirstText="首页"PrevText="上一页"NextText="下一页"LastText="尾页"EmptyMsg="暂时无您所需的数据,查询条件是否满足。"DisplayMsg="当前记录{0}-{1}条共{2}条记录"AutoHeight="true"AfterPageText="页,共{0}页"BeforePageText="第"><Items><ext:ComboBoxID="ComboBox2"runat="server"Width="80"><Items><ext:ListItemText="10"/><ext:ListItemText="20"/><ext:ListItemText="30"/><ext:ListItemText="50"/></Items><SelectedItemValue="20"/><Listeners><SelectHandler="#{pageTool}.pageSize=parseInt(this.getValue());#{pageTool}.doLoad();"/></Listeners></ext:ComboBox></Items></ext:PagingToolbar></BottomBar></ext:GridPanel></Items></ext:Panel></Items></ext:Viewport></div></form>
后台protectedvoidToExcel(objectsender,EventArgse){stringjson=GridData.Value.ToString();StoreSubmitDataEventArgseSubmit=newStoreSubmitDataEventArgs(json,null);XmlNodexml=eSubmit.Xml;this.Response.Clear();this.Response.ContentType="application/vnd.ms-excel";this.Response.AddHeader("Content-Disposition","attachment;filename=submittedData.xls");XslCompiledTransformxtExcel=newXslCompiledTransform();xtExcel.Load(Server.MapPath("Excel.xsl"));xtExcel.Transform(xml,null,this.Response.OutputStream);this.Response.End();}
XSL<xsl:stylesheetversion="1.0"xmlns="urn:schemas-microsoft-com:office:spreadsheet"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt"xmlns:user="urn:my-scripts"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"><xsl:templatematch="/"><Workbookxmlns="urn:schemas-microsoft-com:office:spreadsheet"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"xmlns:html="http://www.w3.org/TR/REC-html40"><xsl:apply-templates/></Workbook></xsl:template><xsl:templatematch="/*"><Worksheet><xsl:attributename="ss:Name"><xsl:value-ofselect="local-name(/*/*)"/></xsl:attribute><Tablex:FullColumns="1"x:FullRows="1"><Row><xsl:for-eachselect="*[position()=1]/*"><Cell><Datass:Type="String"><xsl:value-ofselect="local-name()"/></Data></Cell></xsl:for-each></Row><xsl:apply-templates/></Table></Worksheet></xsl:template><xsl:templatematch="/*/*"><Row><xsl:apply-templates/></Row></xsl:template><xsl:templatematch="/*/*/*"><Cell><Datass:Type="String"><xsl:value-ofselect="."/></Data></Cell></xsl:template></xsl:stylesheet>
Store和GridPanel都是后台动态绑定的列。只剩下20分了求大神帮忙看看急在线等
解决方案
解决方案二:
是分少没人理么。。。。。
解决方案三:
http://www.cnblogs.com/sanshi/archive/2009/08/23/1552533.html
解决方案四:
你试试将GridPanel中的数据现在添加到一个DataTable里面for(inti=0;i<this.GridPanel.Columns.Count;i++){//将表头信息添加到DataTable表头dt2.Columns.Add(this.GridPanel.Columns[i].HeaderText);}for(inti=0;i<dt.Rows.Count;i++){dt2.Rows.Add(dt.Rows[i].ItemArray);//添加数据行}ToExcel(dt2);然后在publicvoidToExcel(DataTabledt){DataGriddgExcel=newDataGrid();dgExcel.DataSource=dt;dgExcel.DataBind();HttpContext.Current.Response.Charset="GB2312";stringfileName=HttpUtility.UrlEncode(Guid.NewGuid().ToString(),System.Text.Encoding.UTF8);stringstr="attachment;filename="+fileName+".xls";HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;HttpContext.Current.Response.ContentType="application/ms-excel";HttpContext.Current.Response.AppendHeader("content-disposition",str);StringWritersw=newStringWriter();HtmlTextWriterhtmTextWriter=newHtmlTextWriter(sw);dgExcel.RenderControl(htmTextWriter);HttpContext.Current.Response.Write("<html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>");stringstyle="<style>td{mso-number-format:"\@";}</style>";//防止导出excel时将以0开头的全数字数据的0去掉HttpContext.Current.Response.Write(style);HttpContext.Current.Response.Write("</head><body>");HttpContext.Current.Response.Write(sw);HttpContext.Current.Response.Write("</body></html>");HttpContext.Current.Response.End();}