/** * 下载带模板的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; } }
标签:map,java,示例,excel,镇级,模板,response,成本 From: https://www.cnblogs.com/gujiajie/p/16789105.html