Use SPRINGMVC to invoke POI jar to export source code for Excel

Source: Internet
Author: User

@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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.