JavaWeb中导出excel文件
来源:互联网 发布:转发小视频软件 编辑:程序博客网 时间:2024/05/16 09:52
在平时做系统项目时,经常会需要做导出功能,不论是导出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;}
这样之后就可以直接点击导出就有效果了。
0 0
- JavaWeb中导出excel文件
- JavaWeb中导出excel文件的简单方法
- JavaWeb中如何导出Excel
- 【javaWeb】Struts 2导出excel文件
- javaWeb导出POI 操作excel文件
- JavaWeb导出excel
- JavaWeb导出Excel出错
- Silverlight中导出Excel文件
- JavaWeb项目导出Excel文件并弹出下载框
- javaweb项目-将数据导出为excel文件思路总结
- Javaweb实现页面上如何导出Excel文件
- 在Javaweb中利用POI导出Excel表格
- javaweb 利用poi 导出excel
- javaWeb导出Excel到客户端
- javaweb--数据导出到excel
- 【JavaWeb】WEB下excel导出
- javaweb使用POI导出导出excel表格
- 将DataSet导出到Excel文件中
- 谈谈iOS面试常提及到的线程间的通信
- 大数开方
- 你真的了解load方法么?
- C++ string split函数实现
- Jsp9个内置对象详解
- JavaWeb中导出excel文件
- Linux学习总结(5)——CentOS常用的目录文件操作命令
- 异步请求网络数据
- 4.spark hash shuffer 解析
- 数据流向可视化
- 物理cpu 逻辑cpu cpu核数
- 关于Linux系统清理/tmp/文件夹的原理
- bootbox自定义dialog、confirm、alert样式,以及基本设置方法setDefaults中可用参数
- 单机搭建spark环境