|
|
@@ -1,4 +1,4 @@
|
|
|
-package com.nb.web.service.bus.utils;
|
|
|
+package com.nb.web.service.bus.excel;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
@@ -21,14 +21,24 @@ import java.util.HashSet;
|
|
|
|
|
|
public class LiquidExcelMergeWriteHandler implements RowWriteHandler {
|
|
|
// 最小行索引
|
|
|
- private int minRowIndex = 4;
|
|
|
- // 不需要合并的列索引(根据ExcelProperty的order值)
|
|
|
- private Set<Integer> excludeColumnIndexes = new HashSet<Integer>() {{
|
|
|
- add(9); // 药品名称 (name) - order = 9
|
|
|
- add(10); // 药品用量 (dose) - order = 10
|
|
|
- add(11); // 药品单位 (unit) - order = 11
|
|
|
- add(12); // 批号 (batchCode) - order = 12
|
|
|
- add(13); // 用法 (use) - order = 13
|
|
|
+ private int minRowIndex = 0;
|
|
|
+ // 需要合并的列索引(根据ExcelProperty的order值)
|
|
|
+ private Set<Integer> includeColumnIndexes = new HashSet<Integer>() {{
|
|
|
+ add(0); // 序号 (num) - order = 1
|
|
|
+ add(1); // 手术名称 (clinicName) - order = 1
|
|
|
+ add(2); // 住院号 (patientCode) - order = 2
|
|
|
+ add(3); // 病人名称 (patientName) - order = 3
|
|
|
+ add(4); // 病区 (ward) - order = 4
|
|
|
+ add(5); // 床号 (bedNo) - order = 5
|
|
|
+ add(6); // 泵别名 (deviceAlias) - order = 6
|
|
|
+ add(7); // 用泵时间 (clinicStartTime) - order = 7
|
|
|
+ add(8); // 撤泵时间 (undoTime) - order = 8
|
|
|
+ add(14); // 总量 (totalDose) - order = 14
|
|
|
+ add(15); // 剩余量 (remainDose) - order = 15
|
|
|
+ add(16); // 废液执行人 (liquidExecutor) - order = 16
|
|
|
+ add(17); // 废液检查人 (liquidChecker) - order = 17
|
|
|
+ add(18); // 废液核对备注 (liquidRemark) - order = 18
|
|
|
+ add(19); // 废液核对时间 (liquidTime) - order = 19
|
|
|
}};
|
|
|
|
|
|
private List<BusLiquidExcelVO> source;
|
|
|
@@ -40,63 +50,102 @@ public class LiquidExcelMergeWriteHandler implements RowWriteHandler {
|
|
|
@Override
|
|
|
public void afterRowDispose(RowWriteHandlerContext context) {
|
|
|
Integer rowIndex = context.getRowIndex();
|
|
|
- if (rowIndex != CollectionUtil.size(source) + 3) {
|
|
|
+ // 只有在处理最后一行时才执行合并操作
|
|
|
+ if (rowIndex != CollectionUtil.size(source)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 设置表头字体和大小
|
|
|
+ setHeaderStyle(context.getWriteSheetHolder().getSheet());
|
|
|
+ // 设置所有单元格垂直居中
|
|
|
+ setVerticalAlignment(context.getWriteSheetHolder().getSheet());
|
|
|
// 自适应列宽
|
|
|
autoSizeColumns(context.getWriteSheetHolder().getSheet());
|
|
|
|
|
|
Integer lastRowIndex = rowIndex;
|
|
|
int lastMergeIndex = lastRowIndex;
|
|
|
int currentIndex = lastRowIndex;
|
|
|
- while (currentIndex >= 4) {
|
|
|
- int preIndex = currentIndex - 1;
|
|
|
- preIndex = preIndex > 4 ? preIndex : 4;
|
|
|
- BusLiquidExcelVO curSource = CollectionUtil.get(source, currentIndex - 4);
|
|
|
- BusLiquidExcelVO preSource = CollectionUtil.get(source, preIndex - 4);
|
|
|
- if (!StrUtil.equals(curSource.getClinicId(), preSource.getClinicId()) || currentIndex == 4) {
|
|
|
- // 合并除指定列外的所有列 (总共20列,从0到19)
|
|
|
- for (int i = 0; i <= 19; i++) {
|
|
|
- // 跳过不需要合并的列
|
|
|
- if (!excludeColumnIndexes.contains(i)) {
|
|
|
- mergeCell(context.getWriteSheetHolder().getSheet(), currentIndex, lastMergeIndex, i, i);
|
|
|
- }
|
|
|
+ while (currentIndex >= 1) {
|
|
|
+ BusLiquidExcelVO curSource = CollectionUtil.get(source, currentIndex -1 );
|
|
|
+ BusLiquidExcelVO preSource = CollectionUtil.get(source, currentIndex -2);
|
|
|
+ if (!StrUtil.equals(curSource.getClinicId(), preSource.getClinicId()) || currentIndex == 1) {
|
|
|
+ // 合并指定的列
|
|
|
+ for (Integer i : includeColumnIndexes) {
|
|
|
+ mergeCell(context.getWriteSheetHolder().getSheet(), currentIndex, lastMergeIndex, i, i);
|
|
|
}
|
|
|
- lastMergeIndex = preIndex;
|
|
|
+ currentIndex--;
|
|
|
+ lastMergeIndex = currentIndex;
|
|
|
+ }else {
|
|
|
+ currentIndex--;
|
|
|
}
|
|
|
- currentIndex--;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void setFontStyle(Sheet sheet) {
|
|
|
- Font font = sheet.getWorkbook().createFont();
|
|
|
- font.setFontHeightInPoints((short) 8); // 设置字体大小为8号
|
|
|
-
|
|
|
+ private void setVerticalAlignment(Sheet sheet) {
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
- style.setFont(font);
|
|
|
-
|
|
|
- // 设置所有单元格的样式
|
|
|
- for (int i = 0; i < sheet.getLastRowNum(); i++) {
|
|
|
+ // 设置垂直居中
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ // 设置水平左对齐
|
|
|
+ style.setAlignment(HorizontalAlignment.LEFT);
|
|
|
+ // 设置边框
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 应用到所有单元格
|
|
|
+ for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
Row row = sheet.getRow(i);
|
|
|
if (row != null) {
|
|
|
for (int j = 0; j < row.getLastCellNum(); j++) {
|
|
|
Cell cell = row.getCell(j);
|
|
|
if (cell != null) {
|
|
|
cell.setCellStyle(style);
|
|
|
+ } else {
|
|
|
+ cell = row.createCell(j);
|
|
|
+ cell.setCellStyle(style);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private void autoSizeColumns(Sheet sheet) {
|
|
|
// 自适应列宽
|
|
|
for (int i = 0; i < 20; i++) { // 20列
|
|
|
- sheet.autoSizeColumn(i);
|
|
|
+// sheet.autoSizeColumn(i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void setHeaderStyle(Sheet sheet) {
|
|
|
+ // 创建表头样式
|
|
|
+ CellStyle headerStyle = sheet.getWorkbook().createCellStyle();
|
|
|
+ Font headerFont = sheet.getWorkbook().createFont();
|
|
|
+ headerFont.setFontName("微软雅黑"); // 设置字体为微软雅黑
|
|
|
+ headerFont.setFontHeightInPoints((short) 10); // 设置字体大小为10号
|
|
|
+ headerFont.setBold(true); // 设置粗体
|
|
|
+ headerStyle.setFont(headerFont);
|
|
|
+ headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
|
|
|
+ headerStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
|
|
|
+ // 设置灰色背景
|
|
|
+ headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ // 设置边框
|
|
|
+ headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 应用表头样式到第一行(表头行)
|
|
|
+ Row headerRow = sheet.getRow(0);
|
|
|
+ if (headerRow != null) {
|
|
|
+ for (int i = 0; i < headerRow.getLastCellNum(); i++) {
|
|
|
+ Cell cell = headerRow.getCell(i);
|
|
|
+ if (cell != null) {
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
private void mergeCell(Sheet sheet, int startRowIndex, int endRowIndex, int startColumnIndex, int endColumnIndex) {
|
|
|
// 开始和结束行数一样
|
|
|
if (startRowIndex == endRowIndex) {
|