首页 > 代码库 > JXLS模板导出EXCEL
JXLS模板导出EXCEL
最近做要做一些报表导出的功能,由于表格内容比较复杂,直接生成excel比较麻烦,所以采用模板的方式来做,可惜自己不了解,也证明了自己技术有多差!通过多次资料,终于找到了适合自己的方法。特此记录,方便以后查找。
maven用到的包
<dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-reader</artifactId> <version>1.0.6</version> </dependency>
Service代码
public void exportChargeCount(String yearMonth, String buildId, HttpServletRequest request,HttpServletResponse response) { String templateFile = request.getSession().getServletContext().getRealPath("/")+"/doc/reportTmp/billingInfo.xls"; Date date = BillingDateUtil.getBillingEndTime(Integer.valueOf(yearMonth.substring(0, 4)),Integer.valueOf(yearMonth.substring(5, 7))); String buildName = dataCenter.getBuildInfoById(buildId).getBuildName(); String fileName = buildName+"("+yearMonth+")客户汇总报表.xls"; List<ChargeCountVO> list = this.reportDao.getChargeCount(DateUtil.getFormatNormal(date), buildId); Map<String, Object> context = new HashMap<>(); XLSTransformer transformer = new XLSTransformer(); float sumMoney = 0; for (ChargeCountVO chargeCountVO : list) { sumMoney += chargeCountVO.getEnergyMoney(); } context.put("billingList", list); context.put("sumMoney", sumMoney); context.put("billingDate", yearMonth); context.put("buildName",buildName ); try { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename="+new String( fileName.getBytes("GBK"), "ISO8859-1" )); Workbook workbook = transformer.transformXLS(new FileInputStream(templateFile), context); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); } catch (ParsePropertyException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
采用键值对的形式
xls模板
导出效果
大家可能会遇到的问题
1、web下载中文名称不显示的问题
2、模板使用公式,下载下来不计算的问题
3、合并单元格的问题
下面逐一解答
1、中文字符转码
response.setHeader("Content-disposition", "attachment;filename="+new String( fileName.getBytes("GBK"), "ISO8859-1" ));
2、重新加载模板公式(通用方法)
/** * * 重新设置单元格计算公式 * * */ private void resetCellFormula(HSSFWorkbook wb) { HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(wb); int sheetNum = wb.getNumberOfSheets(); for (int i = 0; i < sheetNum; i++) { HSSFSheet sheet = wb.getSheetAt(i); int rows = sheet.getLastRowNum() + 1; for (int j = 0; j < rows; j++) { HSSFRow row = sheet.getRow(j); if (row == null) continue; int cols = row.getLastCellNum(); for (int k = 0; k < cols; k++) { HSSFCell cell = row.getCell(k); // if (cell != null) // System.out.println("cell["+j+","+k+"]=:"+cell.getCellType()); if (cell == null) continue; if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { cell.setCellFormula(cell.getCellFormula()); // System.out.println("----公式:"+cell.getCellFormula()); cell = e.evaluateInCell(cell); // System.out.println("-----------"+cell.getNumericCellValue()); } } } } }
使用方法
3、合并单元格
HSSFWorkbook workbook = (HSSFWorkbook )transformer.transformXLS(new FileInputStream(templateFile), context); HSSFSheet sheet = workbook.getSheetAt(0); sheet.addMergedRegion(new CellRangeAddress(起始行号,截至行号,起始列号,截至列号));
四个参数均从0开始
以上就是最近的总结。如果有问题大家可以互相交流。
本文出自 “GUI” 博客,请务必保留此出处http://xingfudehunpo.blog.51cto.com/1843260/1582141
JXLS模板导出EXCEL
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。