JavaWeb中导出excel文件的简单方法

发布于 2020-8-17|标签java
复制链接
下面小妖就为大家带来一篇JavaWeb中导出excel文件的简单方法。小妖觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小妖过来看看吧
在平时做系统项目时,经常会需要做导出功能,不论是导出excel,还是导出cvs文件。我下面的demo是在springmvc的框架下实现的。1.JS中只需要用GET模式请求导出就可以了: ```java $('#word-export-btn').parent().on('click',function(){ var promotionWord = JSON.stringify($('#mainForm').serializeObject()); location.target="_blank" rel="nofollow" href="${ctx}/promotionWord/export?promotionWord="+promotionWord; }); ``` 2.在controller中要做的是将文件以数据流格式输出: ```java @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中需要将数据写入到格式文件中: ```java 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 pWordList; Map 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 list = commonDao.queryList(PROMOTION_WORD_DAO + ".queryExportDataByUser", map); String[] excelHeader = {"关键词", "价格","搜索热度","推广评分","购买热度","曝光量","点击量","点击率","推广时长","花费","平均点击花费","匹配产品数","预估排名","状态"}; for (int i = 0; i 0) for (int i = 0; i 这样之后就可以直接点击导出就有效果了。
冀ICP备17029012号-4 | 版权所有©鲍亚龙 |免责声明  | GIF图库  | NUXT版