JavaWeb中导出excel文件的简单方法
在平时做系统项目时,经常会需要做导出功能,不论是导出excel,还是导出cvs文件。我下面的demo是在springmvc的框架下实现的。
1.JS中只需要用GET模式请求导出就可以了:
$('#word-export-btn').parent().on('click',function(){
varpromotionWord=JSON.stringify($('#mainForm').serializeObject());
location.href="${ctx}/promotionWord/export?promotionWord="+promotionWord;
});
2.在controller中要做的是将文件以数据流格式输出:
@RequestMapping("/export")
publicvoidexport(HttpSessionsession,StringpromotionWord,HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{
UsersessionUser=(User)session.getAttribute("user");
JSONObjectjsonObj=JSONObject.parseObject(promotionWord);
HSSFWorkbookwb=promotionWordService.export(sessionUser.getId(),jsonObj);
response.setContentType("application/vnd.ms-excel");
Calendarcal=Calendar.getInstance();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
StringfileName="word-"+sdf.format(cal.getTime())+".xls";
response.setHeader("Content-disposition","attachment;filename="+fileName);
OutputStreamouputStream=response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
3.在service中需要将数据写入到格式文件中:
publicHSSFWorkbookexport(StringuserId,JSONObjectjsonObj){
HSSFWorkbookwb=newHSSFWorkbook();
HSSFSheetsheet=wb.createSheet("word");
HSSFRowrow=sheet.createRow(0);
HSSFCellStylestyle=wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
List<PromotionWord>pWordList;
Map<String,Object>map=newHashMap<>();
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"));
Longimpression=jsonObj.getLong("impressionCount");
Longclick=jsonObj.getLong("clickCount");
if(impression!=null){
PromotionWordword=newPromotionWord();
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);
}
}elseif(click!=null){
PromotionWordword=newPromotionWord();
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(inti=0;i<excelHeader.length;i++){
HSSFCellcell=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(inti=0;i<list.size();i++){
row=sheet.createRow(i+1);
PromotionWordword=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{
DecimalFormatdf=newDecimalFormat("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());
Stringrank="";
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?"暂停":"启动");
}
returnwb;
}
这样之后就可以直接点击导出就有效果了。
以上就是小编为大家带来的JavaWeb中导出excel文件的简单方法全部内容了,希望大家多多支持毛票票~