@RequestMapping (value = "/result/export")
Public String Export (resultin in,httpservletrequest request,
HttpServletResponse response) throws Exception {
Resultxls Xls=resultservice.getresultxls (in);
Hssfworkbook WB = resultservice.toexcel (xls);
Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8");
Response.setheader ("Content-disposition", "attachment;filename=" + "query" + ". xls");
Response.setheader ("Content-disposition", "Attachment;filename=export.xls");
Response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"), " Iso8859-1 "));
OutputStream Ouputstream = Response.getoutputstream ();
Wb.write (Ouputstream);
Ouputstream.flush ();
Ouputstream.close ();
return null;
}
Extracting the data needed for an Excel file
@Transactional
Public Resultxls Getresultxls (Resultin in) {
String json= This.find (in);
List<map> datalist=gsonutils.getlist (JSON);
Resultxls o=new Resultxls ();
O.setdatalist (dataList);
list<sequencespo> sequencelist = GsonUtils.gson.fromJson (In.getsequences (),
New Typetoken<list<sequencespo>> () {
}.gettype ());
O.setsequenceslist (sequencelist);
return o;
}
Convert to Excel file
@Transactional
Public Hssfworkbook toexcel (Resultxls xls) {
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet sheet = wb.createsheet ("Campaign");
Hssfrow row = Sheet.createrow ((int) 0);
Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (Hssfcellstyle.align_center);
List<map> datalist=xls.getdatalist ();
List<sequencespo> sequenceslist=xls.getsequenceslist ();
Looping column headings
String colname= "";
Row.createcell (0). Setcellvalue ("indicator data");
for (int i = 1; I <= sequenceslist.size (); i++) {
Colname=sequenceslist.get (i). GetDisplayName ();//title Chinese
Row.createcell (i). Setcellvalue (colname);
}
Loop Line
String val= "";
for (int i=0;i<datalist.size (); i++) {
Map Map=datalist.get (i);
row = Sheet.createrow (i + 1);
Val= (String) map.get ("SJ");//First column
Row.createcell (0). Setcellvalue (Val);
Looping columns
for (int j=1;j<=sequenceslist.size (); j + +) {
Val= (String) map.get ("C" +j);
Row.createcell (j). Setcellvalue (Val);
}
}
return WB;
}
Use SPRINGMVC to invoke POI jar to export source code for Excel