/** * 工具类 * * @param workbook * @param sheetNum 第几个sheet * @param sheetTitle sheet的名称 * @param headers 表头 * @param headersEnglish 表头对饮的字段名 * @param result 数据 * @param out * @throws Exception */ public void exportExcel(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers,String[] headersEnglish, List<Map<String,Object>> result, OutputStream out) throws Exception { // 第一步,创建一个webbook,对应一个Excel以xsl为扩展名文件 XSSFSheet sheet = workbook.createSheet(); workbook.setSheetName(sheetNum, sheetTitle); //设置列宽度大小 sheet.setDefaultColumnWidth((short) 30); //第二步, 生成表格第一行的样式和字体 XSSFCellStyle xssfCellStyleHeader = getAndSetXSSFCellStyleHeader(workbook); // 产生表格标题行 XSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { XSSFCell cell = row.createCell((short) i); cell.setCellStyle(xssfCellStyleHeader); // 序号 XSSFRichTextString text = new XSSFRichTextString(headers[i]); cell.setCellValue(text.toString()); } // 第三步:遍历集合数据,产生数据行,开始插入数据 if (result != null) { int index = 1; for (int rownum = 1; rownum < result.size(); rownum++) { row = sheet.createRow(index); int cellIndex = 0; for (int i = 0; i < headersEnglish.length; i++) { XSSFCell cell = row.createCell((int) cellIndex); //当区不到数据时,说明是序号 String value = (result.get(rownum - 1).get(headersEnglish[i])) == null ? rownum + "" : (result.get(rownum - 1).get(headersEnglish[i])) + ""; cell.setCellValue(value); cellIndex++; } index ++; } } setSheet(sheet); } //调用 @RequestMapping("/exportExcelTest") @ResponseBody public Map<String,String> JsonReponseBody(@RequestParam Map<String, String> map,HttpServletResponse response) { Map<String,String> result=new HashMap<String, String>(); // 分季度导出 String periods = map.get("period"); //不同季度的id,用“,”分开 String[] Quarter=periods.split(","); //获取季度的名称,按季度导出 for (int quarter = 0; quarter < Quarter.length; quarter++) { // 获取某个季度的id,一次只查询一个季度的信息集合 map.put("period",Quarter[quarter]); //获取桌面路径,用于下载文件 FileSystemView fsv = FileSystemView.getFileSystemView(); File com = fsv.getHomeDirectory(); // 导出的excel,全文件名 //final String excelExportDestfilepath = com + "/" + "/abc.xlsx"; // 通过季度id获取季度的名称,按季度导出,并将季度名称用作文件名 String excelExportDestfilepath = com + "/" + quarterName(Quarter[quarter]) + ".xlsx"; BasyybqkckController excelExport = null; try { excelExport = new BasyybqkckController(); OutputStream out = new FileOutputStream(excelExportDestfilepath); //表头 String[] nameAry = {"序号", "医院编号", "医院名称", "所属区县", "区县代码", "医院等级", "上报病案数", "填报中", "上报完成率(%)"}; //表头对应的字段 String[] ary = {"xh", "ID", "NAME", "CANT_NAME", "CANT_CODE", "MEDICAL_ORG_GRADE", "FINISHED", "UNFINISHED", "SBWCL"}; // 要导出的数据 List<Map<String, Object>> list = basyybqkckService.getBASBQK(map); //把城市的名称放到数组中,用作不同sheet的名称 String[] cityName = {"济南市", "青岛市", "淄博市", "枣庄市", "东营市", "烟台市", "潍坊市", "济宁市", "泰安市", "威海市", "日照市", "临沂市", "德州市", "聊城市", "滨州市", "菏泽市"}; String[] cityId = {"370100", "370200", "370300", "370400", "370500", "370600", "370700", "370800", "370900", "371000", "371100", "371300", "371400", "371500", "371600", "371700"}; // 存放16个地级市的数据 List listExcel = new ArrayList(); // 按山东省不同的市区导出数据 济南市 370100,青岛市 370200 --> 菏泽市 for (int i = 0; i < cityId.length; i++) { List param = new ArrayList(); // 16个地级市的数据 for (int j = 0; j < list.size(); j++) { Map<String, Object> mapData = list.get(j); String id = list.get(j).get("CANT_DRILL_PARENT_CODE") + ""; if (id.equals(cityId[i])) { param.add(mapExcel(mapData)); } } // 把数据放到数组中,数组中放的是16个地级市的数据 listExcel.add(param); } //3、生成格式是xlsx可存储103万行数据,如果是xls则只能存不到6万行数据 XSSFWorkbook workbook = new XSSFWorkbook(); // 循环导出山东省16个地级市 for (int i = 0; i <cityName.length ; i++) { // workbook // i 第几个sheet // cityName[i] sheet的名称 // headers 表头 // ary 表头对饮的字段名 // listExcel[i] 数据 excelExport.exportExcel(workbook, i, cityName[i], nameAry, ary, (List<Map<String, Object>>) listExcel.get(i), out); } //将所有的数据一起写入,然后再关闭输入流。 workbook.write(out); result.put("data","200"); out.close(); } catch (Exception e) { e.printStackTrace(); result.put("data","300"); } } return result; } |