实现类: public Map<String, Object> downLoadGuiHuaPathById(Integer id) throws IOException { Map<String, Object> mapModel = new HashMap<>(); if (id == null) { throw new ArgumentException("ID不能为空。"); } // 根据规划ID获取一级规划信息 TrGuiHua trGuiHua = trGuiHuaMapper.getGuiHuaById(id); Date startTime = trGuiHua.getStartTime(); Date endTime = trGuiHua.getEndTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); int startYear = Integer.parseInt(sdf.format(startTime)); int endYear = Integer.parseInt(sdf.format(endTime)); String sheetName = trGuiHua.getName(); mapModel.put("fileName", sheetName); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(sheetName); HSSFCellStyle cellStyle = createCellStyle(wb, (short) 11, true); // 处理表头第一行 HSSFRow row1 = sheet.createRow(0); HSSFCell cell11 = row1.createCell(0); cell11.setCellValue(sheetName); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, ((((endYear-startYear)+1)*5))+6)); cell11.setCellStyle(cellStyle); // 处理表头第二行 HSSFRow row2 = sheet.createRow(1); HSSFCell cell21 = row2.createCell(0); cell21.setCellValue("专项规划"); CellRangeAddress cellRangeAddress = new CellRangeAddress(1, 2, 0, 0); sheet.addMergedRegion(cellRangeAddress); cell21.setCellStyle(cellStyle); HSSFCell cell22 = row2.createCell(1); cell22.setCellValue("规划"); sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1)); cell22.setCellStyle(cellStyle); HSSFCell cell23 = row2.createCell(2); cell23.setCellValue("项目"); sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 6)); cell23.setCellStyle(cellStyle); // Cell cell24 = row2.createCell(3); // cell24.setCellValue("负责部门"); // Cell cell25 = row2.createCell(4); // cell25.setCellValue("目标"); // 处理表头第三行 HSSFRow row3 = sheet.createRow(2); String title1="编号,内容,负责部门,开始日期,结束日期,"; for (int i = startYear; i <=endYear ; i++) { title1+="编号,内容,负责部门,开始日期,结束日期,"; HSSFCell cell = row2.createCell((((i - startYear)+1)*5)+2); cell.setCellValue(i+""); CellRangeAddress region = new CellRangeAddress(1,
关于Java poi导出excel 合并单元格问题
于 2022-06-23 15:23:01 首次发布
本文详细介绍了使用Java的POI库如何在导出Excel时进行单元格合并,适用于多层级数据结构的展示需求,提供了一种高效的方法来处理复杂的Excel导出问题。
摘要由CSDN通过智能技术生成