JavaWeb中导出excel文件的简单方法
程序员文章站
2024-03-12 23:16:38
在平时做系统项目时,经常会需要做导出功能,不论是导出excel,还是导出cvs文件。我下面的demo是在springmvc的框架下实现的。
1.js中只需要用get模式请...
在平时做系统项目时,经常会需要做导出功能,不论是导出excel,还是导出cvs文件。我下面的demo是在springmvc的框架下实现的。
1.js中只需要用get模式请求导出就可以了:
$('#word-export-btn').parent().on('click',function(){ var promotionword = json.stringify($('#mainform').serializeobject()); location.href="${ctx}/promotionword/export?promotionword="+promotionword; });
2.在controller中要做的是将文件以数据流格式输出:
@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.在service中需要将数据写入到格式文件中:
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 = {"关键词", "价格","搜索热度","推广评分","购买热度","曝光量","点击量","点击率","推广时长","花费","平均点击花费","匹配产品数","预估排名","状态"}; 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.createrow(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.00%"); }else{ decimalformat df = new decimalformat("0.00%"); 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(10).setcellvalue(word.getavgcost7()+""); row.createcell(11).setcellvalue(word.getmatchcount()); string rank = ""; if(word.getmatchcount() != null && word.getmatchcount() != 0){ if(word.getprospectrank() == null || word.getprospectrank() == 0l){ rank = "其他位置"; }else{ rank = "第"+word.getprospectrank()+"位"; } }else{ rank = "---"; } row.createcell(12).setcellvalue(rank); row.createcell(13).setcellvalue(word.getstatus() == 1 ?"暂停":"启动"); } return wb; }
这样之后就可以直接点击导出就有效果了。
以上就是小编为大家带来的javaweb中导出excel文件的简单方法全部内容了,希望大家多多支持~