From 333da783cb267c9e4ff32116c8990112ce72c55a Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Tue, 8 Jul 2025 08:54:47 +0800 Subject: [PATCH] 55555 --- pom.xml | 8 +- .../CaseplatformCaseExcelController.java | 144 +++++++++++++----- 2 files changed, 111 insertions(+), 41 deletions(-) diff --git a/pom.xml b/pom.xml index c75fbdb..22fe38e 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ 4.2.0 2.12.0 4.1.1 - 5.2.3 + 1.3 1.11.842 2.17.2 5.7.22 @@ -281,12 +281,6 @@ ${easyexcel.version} - - org.apache.poi - poi-ooxml - 5.2.3 - - com.aliyun.oss aliyun-sdk-oss diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/module/business/caseplatformcase/controller/CaseplatformCaseExcelController.java b/sa-admin/src/main/java/net/lab1024/sa/admin/module/business/caseplatformcase/controller/CaseplatformCaseExcelController.java index ff11eec..1d16eda 100644 --- a/sa-admin/src/main/java/net/lab1024/sa/admin/module/business/caseplatformcase/controller/CaseplatformCaseExcelController.java +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/module/business/caseplatformcase/controller/CaseplatformCaseExcelController.java @@ -30,10 +30,7 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.FileOutputStream; import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * 病历表 Controller @@ -97,14 +94,18 @@ public class CaseplatformCaseExcelController { // 结算导出 @ApiOperation("结算导出") - @GetMapping("/caseplatformCase/settlementExcel1") + @GetMapping("/caseplatformCase/settlementExcel") public void exportExpertCaseExceli(HttpServletResponse response, @Valid CaseplatformCaseQueryForm queryForm) { try { List list = caseplatformCaseService.exportExpertCaseExcel(queryForm); List processedList = caseplatformCaseService.exportExpertCaseExcelProcess(list); - // 设置本地文件路径 - String filePath = "./病例数据.xlsx"; + // 总计 + double totalActual = processedList.stream().mapToDouble(e -> Optional.ofNullable(e.getActualAmount()).orElse(0.0)).sum(); + double totalTax = processedList.stream().mapToDouble(e -> Optional.ofNullable(e.getTaxAmount()).orElse(0.0)).sum(); + double totalTotal = processedList.stream().mapToDouble(e -> Optional.ofNullable(e.getTotalAmount()).orElse(0.0)).sum(); + + String filePath = "./人工肝诊疗专家劳务费.xlsx"; FileOutputStream outputStream = new FileOutputStream(filePath); // 字段标题 @@ -125,48 +126,122 @@ public class CaseplatformCaseExcelController { Collections.singletonList("关联病例") ); - // 表头样式 WriteCellStyle headStyle = new WriteCellStyle(); headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headStyle.setVerticalAlignment(VerticalAlignment.CENTER); + headStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); + headStyle.setBorderBottom(BorderStyle.THIN); + headStyle.setBorderTop(BorderStyle.THIN); + headStyle.setBorderLeft(BorderStyle.THIN); + headStyle.setBorderRight(BorderStyle.THIN); - // 内容样式 WriteCellStyle contentStyle = new WriteCellStyle(); contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); + contentStyle.setBorderBottom(BorderStyle.THIN); + contentStyle.setBorderTop(BorderStyle.THIN); + contentStyle.setBorderLeft(BorderStyle.THIN); + contentStyle.setBorderRight(BorderStyle.THIN); - // 1. 创建ExcelWriter ExcelWriter writer = EasyExcel.write(outputStream, ExportExpertCaseExcelVo.class) - .head(head) - .excelType(ExcelTypeEnum.XLSX) - .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, contentStyle)) - .build(); + .head(head) + .excelType(ExcelTypeEnum.XLSX) + .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, contentStyle)) + .build(); WriteSheet writeSheet = EasyExcel.writerSheet("劳务费明细") - .needHead(true) - .relativeHeadRowIndex(2) // 数据从第三行开始 - .build(); + .needHead(true) + .relativeHeadRowIndex(1) // 数据从第3行开始,前2行为标题 + .build(); - // 2. 用 POI 操作 sheet,合并第一行并写入大标题 + // 写数据 + writer.write(processedList, writeSheet); + + // 合并大标题 Workbook workbook = (Workbook) writer.writeContext().writeWorkbookHolder().getWorkbook(); Sheet sheet = workbook.getSheetAt(0); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, head.size() - 1)); - Row row = sheet.getRow(0); - if (row == null) { - row = sheet.createRow(0); + Row titleRow = sheet.createRow(0); + titleRow.setHeightInPoints(30); // 设置标题行高 + Cell titleCell = titleRow.createCell(0); + titleCell.setCellValue("人工肝诊疗病例征集项目专家劳务费表"); + CellStyle titleStyle = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 16); + font.setBold(true); + titleStyle.setFont(font); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleStyle.setBorderRight(BorderStyle.THIN); // 最后一格加右边框 + titleCell.setCellStyle(titleStyle); + + // === 设置行高 === + sheet.getRow(1).setHeightInPoints(25); // 表头行 + for (int i = 2; i <= processedList.size() + 2; i++) { + Row row = sheet.getRow(i); + if (row != null) { + row.setHeightInPoints(22); + } } - Cell cell = row.createCell(0); - cell.setCellValue("人工肝诊疗病例征集项目专家劳务费表"); - // 2. 写入数据 - writer.write(processedList, writeSheet); + // === 设置列宽 === + int[] columnWidths = { + 6, 25, 15, 15, 25, 10, 10, 30, 8, 8, 30, 25, 30, 15 + }; + for (int i = 0; i < columnWidths.length; i++) { + sheet.setColumnWidth(i, columnWidths[i] * 256); // 乘 256 为 Excel 单位 + } + + // 写合计行 + int totalRowIndex = processedList.size() + 2; // +1是数据从第3行开始,+2是表头行数 + Row totalRow = sheet.createRow(totalRowIndex); + totalRow.setHeightInPoints(22); + + // 合计行样式(黑底白字) + CellStyle totalStyle = workbook.createCellStyle(); + Font totalFont = workbook.createFont(); + totalFont.setBold(true); + totalFont.setColor(IndexedColors.WHITE.getIndex()); + totalStyle.setFont(totalFont); + totalStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + totalStyle.setAlignment(HorizontalAlignment.CENTER); + totalStyle.setVerticalAlignment(VerticalAlignment.CENTER); + totalStyle.setBorderTop(BorderStyle.THIN); + totalStyle.setBorderBottom(BorderStyle.THIN); + totalStyle.setBorderLeft(BorderStyle.THIN); + totalStyle.setBorderRight(BorderStyle.THIN); + + // 合并前3列,写“合计” + sheet.addMergedRegion(new CellRangeAddress(totalRowIndex, totalRowIndex, 0, 5)); + Cell cell0 = totalRow.createCell(0); + cell0.setCellValue("合计"); + + // 设置边框样式 + CellStyle borderStyle = workbook.createCellStyle(); + borderStyle.setBorderBottom(BorderStyle.THIN); + borderStyle.setBorderTop(BorderStyle.THIN); + borderStyle.setBorderLeft(BorderStyle.THIN); + borderStyle.setBorderRight(BorderStyle.THIN); + borderStyle.setAlignment(HorizontalAlignment.CENTER); + + // 创建合计单元格并添加边框 + for (int i = 0; i < head.size(); i++) { + Cell cell = totalRow.getCell(i); + if (cell == null) { + cell = totalRow.createCell(i); + } + cell.setCellStyle(borderStyle); + } + + // 实发(列 G = index 6)、个税(I = 8)、应发(J = 9) + totalRow.getCell(6).setCellValue(totalActual); + totalRow.getCell(8).setCellValue(totalTax); + totalRow.getCell(9).setCellValue(totalTotal); + writer.finish(); + outputStream.close(); - // 6通知浏览器以附件的形式下载处理,设置返回头要注意文件名有中文 - String fileName = URLEncoder.encode("病例数据", "UTF-8").replaceAll("\\+", "%20"); - response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); - response.setContentType("multipart/form-data"); - response.setCharacterEncoding("utf-8"); + System.out.println("Excel 导出完成,路径: " + filePath); } catch (Exception e) { e.printStackTrace(); } @@ -174,7 +249,7 @@ public class CaseplatformCaseExcelController { // 结算导出 @ApiOperation("结算导出") - @GetMapping("/caseplatformCase/settlementExcel") + @GetMapping("/caseplatformCase/settlementExcel1") public void exportExpertCaseExcel(HttpServletResponse response, @Valid CaseplatformCaseQueryForm queryForm) { try { List list = caseplatformCaseService.exportExpertCaseExcel(queryForm); @@ -215,11 +290,12 @@ public class CaseplatformCaseExcelController { bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 3设置表格sheet样式 - WriteSheet sheet = EasyExcel.writerSheet().head(head).sheetNo(1).build(); + WriteSheet sheet = EasyExcel.writerSheet().sheetNo(1).build(); // 4拿到表格处理对象 - ExcelWriter writer = EasyExcel.write(response.getOutputStream()).needHead(true).excelType(ExcelTypeEnum.XLSX) -// ExcelWriter writer = EasyExcel.write(outputStream).needHead(true).excelType(ExcelTypeEnum.XLSX) +// ExcelWriter writer = EasyExcel.write(response.getOutputStream()).needHead(true).excelType(ExcelTypeEnum.XLSX) + ExcelWriter writer = EasyExcel.write(outputStream).needHead(true).excelType(ExcelTypeEnum.XLSX) // 设置单元格的风格样式 + .head(head) .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, bodyStyle)) .build();