When you do a system project, you often need to do the export function, whether it is export Excel, or export CVS files. My demo below is implemented in the framework of SPRINGMVC.
1.JS only need to use the Get mode request export:
$ (' #word-export-btn '). Parent (). On (' click ', Function () {
var promotionword = json.stringify (' #mainForm '). SerializeObject ());
location.href= "${ctx}/promotionword/export?promotionword=" +promotionword;
});
2. What to do in the controller is to output the file in Data flow format:
@RequestMapping ("/export") public
Void Export (HttpSession session, String Promotionword, HttpServletRequest Request, HttpServletResponse response) throws IOException {
user Sessionuser = (user) Session.getattribute ("User");
Jsonobject jsonobj = Jsonobject.parseobject (Promotionword);
Hssfworkbook wb = Promotionwordservice.export (Sessionuser.getid (), jsonobj);
Response.setcontenttype ("application/vnd.ms-excel");
Calendar cal = Calendar.getinstance ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
String fileName = "word-" + Sdf.format (Cal.gettime ()) + ". xls";
Response.setheader ("Content-disposition", "attachment;filename=" + filename);
OutputStream Ouputstream = Response.getoutputstream ();
Wb.write (ouputstream);
Ouputstream.flush ();
Ouputstream.close ();
}
3. in the service you need to write data to the format file:
Public Hssfworkbook Export (String userId, Jsonobject jsonobj) {Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet sheet = wb.createsheet ("word");
Hssfrow row = sheet.createrow (0);
Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (Hssfcellstyle.align_center);
List<promotionword> pwordlist;
map<string, object> map = new hashmap<> ();
Map.put ("UserId", userId);
Map.put ("Checkexistrule", Jsonobj.getstring ("Checkexistrule"));
Map.put ("Status", Jsonobj.getstring ("status"));
Map.put ("Qsstar", Jsonobj.getstring ("Qsstar"));
Map.put ("Impressioncount", Jsonobj.getstring ("Impressioncount"));
Map.put ("Selectgroupid", Jsonobj.getstring ("Selectgroupid"));
Map.put ("Ischeck", Jsonobj.getstring ("Ischeck"));
Map.put ("word", jsonobj.getstring ("word"));
Long impression = Jsonobj.getlong ("Impressioncount");
Long click = Jsonobj.getlong ("Clickcount"); if (impression!= null) {Promotionword word = new Promotionword (); Word.setcreatedby (userId); Word.setimpressioncounT7 (impression);
Pwordlist = gettwentypercentlists (word); if (pwordlist!= null && pwordlist.size () > 0) {map.put ("Impressioncount", Pwordlist.get (Pwordlist.size ()-1)
. Getimpressioncount ()); }else{map.put ("Impressioncount", 1);}}
else if (click!= null) {Promotionword word = new Promotionword (); Word.setcreatedby (userId); word.setclickcount7 (click);
Pwordlist = gettwentypercentlists (word); if (pwordlist!= null && pwordlist.size () > 0) {map.put ("Clickcount", Pwordlist.get (Pwordlist.size ()-1).
GetClickCount ());
}else{map.put ("Clickcount", 1);}}
list<promotionword> list = commondao.querylist (Promotion_word_dao + ". Queryexportdatabyuser", map); String[] Excelheader = {"keywords", "price", "Search Fever", "Promotion score", "Buy heat", "exposure", "clicks", "click rate", "Promotion Time", "cost", "average click spend", "number of matching products", "Estimated rank", "status
"};
for (int i = 0; i < excelheader.length i++) {Hssfcell cell = Row.createcell (i);
Cell.setcellvalue (Excelheader[i]);
Cell.setcellstyle (style); if (i = = 0) {sheet.setcOlumnwidth (0, 30*256);
}else{sheet.setcolumnwidth (i, 10*256); } if (list!= null && list.size () > 0) for (int i = 0; i < list.size (); i++) {row = Sheet.crea
Terow (i + 1);
Promotionword Word = list.get (i);
Row.createcell (0). Setcellvalue (Word.getword ());
Row.createcell (1). Setcellvalue (Word.getprice () + "");
Row.createcell (2). Setcellvalue (Word.getsearchcount ());
Row.createcell (3). Setcellvalue (Word.getqsstar ());
Row.createcell (4). Setcellvalue (Word.getbuycount ());
Row.createcell (5). Setcellvalue (Word.getimpressioncount7 ());
Row.createcell (6). Setcellvalue (Word.getclickcount7 ());
if (word.getclickcount7 () = = 0L) {Row.createcell (7). Setcellvalue ("0%");
}else{DecimalFormat df = new DecimalFormat ("0%"); Row.createcell (7). Setcellvalue (Df.format (double.valueof (word.getclickcount7 ())/double.valueof (
WORD.GETIMPRESSIONCOUNT7 ()))); } Row.createcell (8). SETcellvalue (Word.getonlinetime7 ());
Row.createcell (9). Setcellvalue (Word.getcost7 () + "");
Row.createcell (Setcellvalue). (Word.getavgcost7 () + "");
Row.createcell. Setcellvalue (Word.getmatchcount ());
String rank = ""; if (Word.getmatchcount ()!= null && word.getmatchcount ()!= 0) {if (Word.getprospectrank () = NULL | | word.get
Prospectrank () = = 0L) {rank = "other location";
}else{rank = "First" +word.getprospectrank () + "bit";
}}else{rank = "---";
Row.createcell. Setcellvalue (rank); Row.createcell Setcellvalue (word.getstatus () = 1?
Pause ":" "start");
return to WB; }
After that, you can directly click on the export will have effect.
The above is a small series for you to bring the javaweb in the export of Excel files in the simple method of all content, I hope that we support cloud Habitat Community ~