This commit is contained in:
wucongxing8150 2025-07-08 08:54:47 +08:00
parent e47844559d
commit 333da783cb
2 changed files with 111 additions and 41 deletions

View File

@ -38,7 +38,7 @@
<easypoi.version>4.2.0</easypoi.version>
<xerces.version>2.12.0</xerces.version>
<poi-scratchpad.version>4.1.1</poi-scratchpad.version>
<poi-ooxml-schemas.version>5.2.3</poi-ooxml-schemas.version>
<poi-ooxml-schemas.version>1.3</poi-ooxml-schemas.version>
<aws-java-sdk.version>1.11.842</aws-java-sdk.version>
<log4j-spring-boot.version>2.17.2</log4j-spring-boot.version>
<hutool.version>5.7.22</hutool.version>
@ -281,12 +281,6 @@
<version>${easyexcel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>

View File

@ -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<ExportExpertCaseExcelVo> list = caseplatformCaseService.exportExpertCaseExcel(queryForm);
List<ExportExpertCaseExcelVo> 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<ExportExpertCaseExcelVo> 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();