1 Reception response
Onexcelbtnclick: function (button, e, eopts) { var gridpanel = this.getrungrid (); var cm=gridpanel.columns; var data=[]; var store = gridpanel.getstore (); store.each (function (record) { data.push (record.data); },this); var headerIndex=[]; Ext.Array.forEach ( cm, function (item) { if (item.xtype == "GridColumn") { &Nbsp; headerindex.push (Item.dataindex); } }); var headtext=[]; ext.array.foreach ( cm, function (item) { if (Item.xtype == "GridColumn") { headtext.push (Item.text); } }); ext.ajax.request ({ disablecaching : true , url: '. /.. /sys/myexportexcel!exportexcel.action ', method: ' POST ', params: { excledata:ext.json.encode (data), excleheader:ext.json.encode ( Headerindex), ExcleText:Ext.JSON.encode (Headtext) } }); },
2 Background parsing data
package com.kl.action.sys;import java.io.outputstream;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import net.sf.json.jsonarray;import net.sf.json.jsonobject;import org.apache.poi.hssf.usermodel.hssfcell; Import org.apache.poi.hssf.usermodel.hssfrow;import org.apache.poi.hssf.usermodel.hssfsheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.namespace;import org.apache.struts2.interceptor.servletrequestaware;import org.apache.struts2.interceptor.servletresponseaware;import org.apache.struts2.interceptor.sessionaware; import com.kl.action.parentaction;import com.opensymphony.xwork2.actionsupport; @Namespace ("/sys") @ Action (value = "Myexportexcel") public class exportexcel extends actionsupport implements servletrequestaware, servletresponseaware { private string excledata; private String excleHeader; private String excletext; protected httpservletrequest request; protected HttpServletResponse response; public string getexcledata () { return excleData; } public Void setexcledata (String excledata) { this.excledata = excledata; } public string Getexcleheader () { return excleHeader; } pUblic void setexcleheader (String excleheader) { this.excleHeader = excleHeader; } public String getexcletext () { return excleText; } public void setexcletext (String excleText) { this.excletext = excletext; } public void exportexcel () { Jsonarray excle = jsonarray.fromobject (This.excledata); hssfworkbook workbook = new hssfworkbook (); hssfsheet sheet = workbook.createsheet (); // Parsing table Header &nbsP; jsonarray dataheader = jsonarray.fromobject ( This.excletext); hssfrow headrow = Sheet.createrow (0); for (int col = 0; Col < dataheader.size (); col++) { string mycell = dataheader.getstring (COL); hssfcell cell = headrow.createcell (COL); // define cells As String types cell.setcelltype (hssfcell.cell_type_string); // enter some content in the cell cell.setCellvalue (MyCell); } int r=0; for (object o : excle) { JSONObject excleObj = ( Jsonobject) o; hssfrow row= Sheet.createrow (short) r+1); //parsing Columns jsonarray dataindex = jsonarray.fromobject (This.excleHeader ); for (Int col=0;col<dataindex.size (); col++) { &nBsp; string mycell=dataindex.getstring (COL); mycell=excleobj.getstring (MyCell ); hssfcell cell = row.createcell (COL); // define cells As String types cell.setcelltype (hssfcell.cell_type_string); // enter some content in the cell cell.setcellvalue (MyCell); } r++; }; // Save the data in Excel format with Response response.reset (); response.setcontenttype ("Application/msexcel;charset=utf-8"); response.setcharacterencoding ("UTF-8"); try { Response.AddHeader ("Content-disposition", "attachment;filename=\" " + new string (" Test " + ". xls "). GetBytes (" UTF-8 "), "Iso8859_1") + "\" "); outputstream out = response.getoutputstream (); workbook.write (out); out.flush (); out.close (); } catch ( Exception e) { E.printstacktrace (); } } @Override public void setservletresponse (httpservletresponse response) { this.response = response; } @oVerride public void setservletrequest (httpservletrequest request) { this.request = request; } }
Example relies on Apache POI component
Please refer to the official case and reference document
Http://extjs.com/forum/showthread.php?t=32400&highlight=excel
http://yeakyang.blog.163.com/blog/static/6413081200961611437835/
Operation Result:
ExtJS Exporting Excel