java根据模板excel导出pdf和excel (easypoi)示例
/** * 下载带模板的excel * @param response * @param map 数据map key需与模板中对应 * @param templateUrl 模板excel路径 * @param titleName 标题名字 */ public static void getExcelPlus(HttpServletResponse response, HashMap<String,Object> map, String templateUrl, String titleName) { //取到要导出的模板 TemplateExportParams params; //成本报表用电量成本 params = new TemplateExportParams(templateUrl); Workbook workbook = ExcelExportUtil.exportExcel(params, map); ServletOutputStream out = null; String fileName = titleName + ".xlsx"; //防止中文乱码 String fileNameUrl; try { fileNameUrl = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } try { //流的形式传输数据 response.setHeader("content-type", "application/octet-stream"); //防止中文乱码 response.setHeader("content-disposition", "attachment;filename=" + fileNameUrl + ";" + "filename*=utf-8''" + fileNameUrl); out = response.getOutputStream(); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } finally { if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * 下载带excel模板的pdf * @param response * @param map 数据map key需与模板中对应 * @param templateUrl 模板excel路径 */ public static void getPdfReportPlus(HttpServletResponse response, HashMap<String,Object> map, String templateUrl) { //取到要导出的模板 TemplateExportParams params; //成本报表用电量成本 params = new TemplateExportParams(templateUrl); Workbook workbook = ExcelExportUtil.exportExcel(params, map); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //将excel文件写入到新的输出流 try { workbook.write(outputStream); } catch (IOException e) { throw new RuntimeException(e); } //将字节数组放置到内存里面 ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); com.spire.xls.Workbook workbook1 = new com.spire.xls.Workbook(); workbook1.loadFromStream(inputStream); //pdf 自适应屏幕大小 workbook1.getConverterSetting().setSheetFitToWidth(true); //通过流的形式输出保存 try { workbook1.saveToStream(response.getOutputStream(), FileFormat.PDF); } catch (IOException e) { throw new RuntimeException(e); } }
TownDrugCostReportVO townDrugCostReportVO = townDrugCostsReportService.drugCosts(dto); HashMap<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(townDrugCostReportVO), HashMap.class); List<TownDrugCostReportVO.Details> detailsList = JSONObject.parseArray(JSONObject.toJSONString(map.get("detailsList")), TownDrugCostReportVO.Details.class); //序号 int id = 1; for (TownDrugCostReportVO.Details details : detailsList) { details.setId(id++ + ""); } map.put("detailsList", detailsList); switch (dto.getDownLoadType()) { case 1: if(dto.getType() == 1) { PdfUtil.getPdfReportPlus(response, map, "static/template/village/镇级成本报表药剂成本-月.xlsx"); break; }else if (dto.getType() == 2) { PdfUtil.getPdfReportPlus(response, map, "static/template/village/镇级成本报表药剂成本-季.xlsx"); break; }else if (dto.getType() == 3) { PdfUtil.getPdfReportPlus(response, map, "static/template/village/镇级成本报表药剂成本-年.xlsx"); break; } case 2: if(dto.getType() == 1) { ExcelUtil.getExcelPlus(response, map, "static/template/village/镇级成本报表药剂成本-月.xlsx", "镇级药剂月成本报表"); break; }else if (dto.getType() == 2) { ExcelUtil.getExcelPlus(response, map, "static/template/village/镇级成本报表药剂成本-季.xlsx", "镇级药剂季成本报表"); break; }else if(dto.getType() == 3) { ExcelUtil.getExcelPlus(response, map, "static/template/village/镇级成本报表药剂成本-年.xlsx", "镇级药剂年成本报表"); break; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了