Javaweb导出Excel文件它是一些系统中常见的功能,也是一个重要的功能。例如,医院医生站、护士站、门急诊电子病历绩效考核等系统将被导出Excel表的功能,方便客户和医生阅读。然后javaweb怎么导出至于Excel文件,我们来看看大致步骤。
首先,让我们来看看jsp页面中js部分导出Excel表的主要功能:$('#word-export-btn').parent().on('click',function(){ var promotionWord = JSON.stringify($('#mainForm').serializeObject()); location.href="${ctx}/promotionWord/export?promotionWord="+promotionWord; });
Jsp页面可以通过get()方法导出。
然后看看控制层导出Excel表在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(); }
在Controller类中需要做的功能是以数据流格式输出文件。
最后是逻辑层Excel导出在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 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.setimpresioncount7(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 < 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.getimpresionCount7(); 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.getimpresionCount7()))); } 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; }
Service类的主要功能是将数据写入格式文件。
以上就是javaweb导出Excel文件的简单步骤,也是Excel表导出的核心功能,大家赶紧试试吧。