1. Using the POI component to implement Excel export functionality
Get a list of issues
List<suggestion> targetstocklist = suggestionservice.getsuggestionlist (map); //Create a new excel Hssfworkbook workbook = new hssfworkbook (); //Create sheet page HSSFSheet sheet = Workbook.createsheet (); //sheet page name workbook.setsheetname (0, "targetstocklist"); //Create Header page HSSFHeader header = Sheet.getheader (); //Set Title Center header.setcenter ("title"); //Setting the first behavior header hssfrow row = sheet.createrow (0); hssfcell cell0 = row.createcell (Short.valueof ("0")); hssfcell cell1 = row.createcell (Short.valueOf ("1")); hssfcell cell2 = row.createcell (Short.valueOf ("2")); // Set Character Sets cell0.setencoding (hssfcell.encoding_utf_16); cell1.setencoding (hssfcell.encoding_utf_16); cell2.setencoding (hssfcell.encoding_utf_16); cell0.setcellvalue ("question title"); cell1.seTcellvalue ("Problem description"); cell2.setcellvalue ("Feedback Time"); if (Targetstocklist != null && !targetstocklist.isempty ()) { for (int i = 0; i < Targetstocklist.size (); i++) { suggestion targetstock = targetstocklist.get (i); row = Sheet.createrow (i + 1); cell0 = row.createcell (Short.valueof ("0")); cell1 = row.createcell (Short.valueof ("1")); cell2 = row.createcell (Short.valueOf ("2" )); // Set Character sets cell0.setencoding ( HSSFCELL.ENCODING_UTF_16); cell1.setencoding (hssfcell.encoding_utf_16); cell2.setencoding (hssfcell.encoding_utf_16); &nbsP; cell0.setcellvalue (Targetstock.gettype ()); cell1.setcellvalue (TargetStock.getContent ()) ; Cell2.setcellvalue (Targetstock.getpublishtime ()); sheet.setcolumnwidth (short) 0, (short) 4000); Sheet.setcolumnwidth (short) 1, (short) 4000); sheet.setcolumnwidth (short) 2, (short) 4000); } } //saving data in Excel format via response response.reset (); Response.setcontenttype ("Application/msexcel;charset=utf-8"); Try { response.addheader (" Content-disposition ", " attachment;filename=\ "" + new string ("User feedback form" + ". xls"). GetBytes ("GBK"), "Iso8859_1") + "\" "); outputstream out = response.getoutputstream (); workbook.write (out); out.flush (); out.close (); } catch ( Exception e) { E.printstacktrace (); } return null;
2.ExtJS call this function to implement the export of Excel
We know that in the interface through a button to achieve Excel export, ExtJS button is generally through the Ext.Ajax.request asynchronous request to implement data submission. But here we can't use asynchronous calls, we need to prompt the user directly to save or open the document. How to achieve it?
In fact, it's very simple, we can easily implement this feature if we understand that ExtJS is a JavaScript development. We can implement the function Call of Excel by writing this code in the trigger event of the button.
Initialize the Excel Export button
var exportexcel = ext.get (' Exportexcel '); Exportexcel.on (' click ', Exportbuttonclick); function Exportbuttonclick () {window.location.href = Ext.context + '/admin/suggestion.do?method=exporttargetlist ' ;};