From 3a7c6aa4e32d274df7640f4d0c42037c70b6b59f Mon Sep 17 00:00:00 2001 From: haomingming Date: Sun, 4 Jan 2026 13:57:15 +0800 Subject: [PATCH] dd --- .../CaseplatformCaseExcelController.java | 191 +----------------- 1 file changed, 9 insertions(+), 182 deletions(-) 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 030ef71..9f4fff3 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 @@ -8,7 +8,6 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import net.lab1024.sa.admin.module.app.medicalrecord.service.MedicalRecordService; import net.lab1024.sa.admin.module.business.caseplatformcase.converter.CustomExcelWriteHandler; import net.lab1024.sa.admin.module.business.caseplatformcase.domain.form.CaseplatformCaseQueryForm; import net.lab1024.sa.admin.module.business.caseplatformcase.domain.vo.EasyExcelCaseDetailVO; @@ -19,16 +18,13 @@ import net.lab1024.sa.common.common.exception.BusinessException; import net.lab1024.sa.common.module.support.operatelog.annoation.OperateLog; import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; -import java.io.FileOutputStream; import java.net.URLEncoder; import java.util.*; @@ -95,170 +91,6 @@ public class CaseplatformCaseExcelController { } // 结算导出 -// @ApiOperation("结算导出") -// @GetMapping("/caseplatformCase/settlementExcel") -// @PreAuthorize("@saAuth.checkPermission('case-system:case:exportExpertCaseExceli')") -// public void exportExpertCaseExceli(HttpServletResponse response, @Valid CaseplatformCaseQueryForm queryForm) { -// try { -// List list = caseplatformCaseService.exportExpertCaseExcel(queryForm); -// List processedList = caseplatformCaseService.exportExpertCaseExcelProcess(list); -// -// // 总计 -// 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); -// -// // 字段标题 -// List> head = Arrays.asList( -// Collections.singletonList("序号"), -// Collections.singletonList("银行"), -// Collections.singletonList("账号所在省份"), -// Collections.singletonList("账户所在地市"), -// Collections.singletonList("卡号"), -// Collections.singletonList("姓名"), -// Collections.singletonList("实发"), -// Collections.singletonList("备注"), -// Collections.singletonList("个税"), -// Collections.singletonList("应发"), -// Collections.singletonList("单位"), -// Collections.singletonList("电话"), -// Collections.singletonList("身份证号"), -// 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); -// -//// ExcelWriter writer = EasyExcel.write(outputStream, ExportExpertCaseExcelVo.class) -// ExcelWriter writer = EasyExcel.write(response.getOutputStream(), ExportExpertCaseExcelVo.class) -// .head(head) -// .excelType(ExcelTypeEnum.XLSX) -// .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, contentStyle)) -// .build(); -// -// WriteSheet writeSheet = EasyExcel.writerSheet("劳务费明细") -// .needHead(true) -// .relativeHeadRowIndex(1) // 数据从第3行开始,前2行为标题 -// .build(); -// -// // 写数据 -// 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 titleRow = sheet.createRow(0); -// Row titleRow = sheet.getRow(0); -// if (titleRow == null) { -// 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); -// } -// } -// -// // === 设置列宽 === -// 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); -// -// // 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"); -// writer.finish(); -// -// outputStream.close(); -// -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } @ApiOperation("结算导出") @GetMapping("/caseplatformCase/settlementExcel") @PreAuthorize("@saAuth.checkPermission('case-system:case:exportExpertCaseExceli')") @@ -290,7 +122,6 @@ public class CaseplatformCaseExcelController { Collections.singletonList("关联病例") ); - // 1. 配置样式策略 WriteCellStyle headStyle = new WriteCellStyle(); headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headStyle.setVerticalAlignment(VerticalAlignment.CENTER); @@ -308,37 +139,33 @@ public class CaseplatformCaseExcelController { contentStyle.setBorderLeft(BorderStyle.THIN); contentStyle.setBorderRight(BorderStyle.THIN); - // 2. 初始化 ExcelWriter(仅使用响应输出流,移除本地文件输出) + // 使用 CustomExcelWriteHandler 在正确的时机创建标题行和合计行 + CustomExcelWriteHandler customHandler = new CustomExcelWriteHandler(totalActual, totalTax, totalTotal, processedList.size()); + ExcelWriter writer = EasyExcel.write(response.getOutputStream(), ExportExpertCaseExcelVo.class) .head(head) .excelType(ExcelTypeEnum.XLSX) .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, contentStyle)) - // 关键:注册自定义处理器,提前处理标题、样式、合计等 - .registerWriteHandler(new CustomExcelWriteHandler(totalActual, totalTax, totalTotal, processedList.size())) + .registerWriteHandler(customHandler) .build(); - // 3. 配置 Sheet(数据从第2行开始,第0行是大标题,第1行是表头) WriteSheet writeSheet = EasyExcel.writerSheet("劳务费明细") .needHead(true) - .relativeHeadRowIndex(1) // 表头行是第1行,数据从第2行开始 + .relativeHeadRowIndex(1) // 数据从第3行开始,前2行为标题 .build(); - // 4. 先写数据(此时自定义处理器会在写入前后处理样式/标题/合计) + // 写数据 writer.write(processedList, writeSheet); - // 5. 响应头配置(中文文件名编码) + // 6通知浏览器以附件的形式下载处理,设置返回头要注意文件名有中文 String fileName = URLEncoder.encode("病例数据", "UTF-8").replaceAll("\\+", "%20"); - response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); - response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); + response.setContentType("multipart/form-data"); response.setCharacterEncoding("utf-8"); - - // 6. 完成写入 writer.finish(); } catch (Exception e) { e.printStackTrace(); - // 补充:抛出异常让全局异常处理器处理,避免前端无响应 - throw new RuntimeException("导出Excel失败:" + e.getMessage(), e); } }