11-18-初版
This commit is contained in:
@ -1770,7 +1770,7 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建模板完整结构(修正行号)
|
||||
* 构建模板完整结构(添加自动调整行高)
|
||||
*/
|
||||
private static void buildTemplateFullStructure(Sheet sheet, Workbook workbook) {
|
||||
// 创建所有需要的样式
|
||||
@ -1823,6 +1823,9 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
|
||||
// 15. 确保所有单元格都有边框
|
||||
ensureAllCellsHaveBorders(sheet, workbook);
|
||||
|
||||
// 16. 自动调整行高以适应内容
|
||||
autoAdjustRowHeights(sheet);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1885,7 +1888,7 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主表头行(第4行)- 彻底修复合并区域冲突
|
||||
* 创建主表头行(第4行)- 修复重叠问题
|
||||
*/
|
||||
private static void createMainHeaderRow(Sheet sheet, CellStyle headerStyle, CellStyle contentStyle) {
|
||||
Row headerRow = sheet.createRow(3);
|
||||
@ -1898,8 +1901,8 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
contentHeader.setCellValue("工程实施主要情况");
|
||||
contentHeader.setCellStyle(headerStyle);
|
||||
|
||||
// 关键修复:先设置B4:I4合并,避免与J3:J4、K3:K4冲突
|
||||
addMergedRegionSafe(sheet, 3, 3, 1, 8); // B4:I4合并
|
||||
// 关键修复:B4:I4合并已经在setupAllSmallMergeRegions中设置,这里不再重复设置
|
||||
// 只设置单元格内容,不设置合并区域
|
||||
|
||||
// J4和K4单独设置(不合并)
|
||||
Cell emptyJ4 = headerRow.createCell(9);
|
||||
@ -1913,8 +1916,6 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
// L4单元格
|
||||
Cell emptyL4 = headerRow.createCell(11);
|
||||
emptyL4.setCellStyle(contentStyle);
|
||||
|
||||
// 注意:J3:J4和K3:K4合并已经在setupDateMergeRegions中设置,这里不再重复设置
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2338,7 +2339,9 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
}
|
||||
}
|
||||
|
||||
// 样式创建方法保持不变
|
||||
/**
|
||||
* 创建标题样式(支持自动换行)
|
||||
*/
|
||||
private static CellStyle createTitleCellStyle(Workbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
Font font = workbook.createFont();
|
||||
@ -2352,9 +2355,13 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setWrapText(true); // 启用自动换行
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建表头样式(支持自动换行)
|
||||
*/
|
||||
private static CellStyle createHeaderCellStyle(Workbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
Font font = workbook.createFont();
|
||||
@ -2370,9 +2377,13 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
||||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
style.setWrapText(true); // 启用自动换行
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建内容样式(支持自动换行和自动调整行高)
|
||||
*/
|
||||
private static CellStyle createContentCellStyle(Workbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
Font font = workbook.createFont();
|
||||
@ -2380,15 +2391,18 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
font.setFontHeightInPoints((short)10);
|
||||
style.setFont(font);
|
||||
style.setAlignment(HorizontalAlignment.LEFT);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.TOP); // 改为顶部对齐,便于自动换行
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setWrapText(true);
|
||||
style.setWrapText(true); // 启用自动换行
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建表头样式(支持自动换行)
|
||||
*/
|
||||
private static CellStyle createTableHeaderCellStyle(Workbook workbook) {
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
Font font = workbook.createFont();
|
||||
@ -2404,11 +2418,12 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
|
||||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
style.setWrapText(true); // 启用自动换行
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全添加合并区域(简化版,只用于后续的大范围合并)
|
||||
* 安全添加合并区域(修复重叠检测逻辑)
|
||||
*/
|
||||
private static void addMergedRegionSafe(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
|
||||
if (firstRow == lastRow && firstCol == lastCol) {
|
||||
@ -2419,22 +2434,88 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
|
||||
// 检查是否与现有合并区域重叠
|
||||
List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
|
||||
boolean hasOverlap = false;
|
||||
|
||||
for (CellRangeAddress existingRegion : mergedRegions) {
|
||||
if (regionsOverlap(newRegion, existingRegion)) {
|
||||
// 如果是完全相同的区域,跳过(可能是重复设置)
|
||||
if (newRegion.equals(existingRegion)) {
|
||||
System.out.println("跳过重复的合并区域: " + newRegion.formatAsString());
|
||||
return;
|
||||
}
|
||||
System.err.println("跳过重叠的合并区域: " + newRegion.formatAsString() +
|
||||
" 与 " + existingRegion.formatAsString());
|
||||
return;
|
||||
hasOverlap = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
sheet.addMergedRegion(newRegion);
|
||||
System.out.println("成功添加合并区域: " + newRegion.formatAsString());
|
||||
} catch (IllegalStateException e) {
|
||||
System.err.println("添加合并区域失败: " + newRegion.formatAsString() + " - " + e.getMessage());
|
||||
if (!hasOverlap) {
|
||||
try {
|
||||
sheet.addMergedRegion(newRegion);
|
||||
System.out.println("成功添加合并区域: " + newRegion.formatAsString());
|
||||
} catch (IllegalStateException e) {
|
||||
System.err.println("添加合并区域失败: " + newRegion.formatAsString() + " - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动调整行高以适应内容
|
||||
*/
|
||||
private static void autoAdjustRowHeights(Sheet sheet) {
|
||||
for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
|
||||
Row row = sheet.getRow(rowNum);
|
||||
if (row != null) {
|
||||
// 计算该行中所有单元格的最大行数
|
||||
int maxLines = 1;
|
||||
for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
|
||||
Cell cell = row.getCell(cellNum);
|
||||
if (cell != null && cell.getStringCellValue() != null) {
|
||||
String cellValue = cell.getStringCellValue();
|
||||
// 根据内容长度和列宽估算行数
|
||||
int estimatedLines = estimateLineCount(cellValue, sheet.getColumnWidth(cellNum));
|
||||
maxLines = Math.max(maxLines, estimatedLines);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置行高(每行约15点)
|
||||
float rowHeight = Math.max(20f, maxLines * 15f); // 最小高度20,根据行数调整
|
||||
row.setHeightInPoints(rowHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 估算文本在指定列宽下的行数
|
||||
*/
|
||||
private static int estimateLineCount(String text, int columnWidth) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 估算每个字符的宽度(近似值)
|
||||
double charWidth = 256; // Excel中字符宽度的近似值
|
||||
double availableWidth = columnWidth;
|
||||
|
||||
String[] lines = text.split("\n");
|
||||
int totalLines = 0;
|
||||
|
||||
for (String line : lines) {
|
||||
if (line.isEmpty()) {
|
||||
totalLines++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 估算该行需要的行数
|
||||
double lineWidth = line.length() * charWidth;
|
||||
int linesNeeded = (int) Math.ceil(lineWidth / availableWidth);
|
||||
totalLines += Math.max(1, linesNeeded);
|
||||
}
|
||||
|
||||
return Math.max(1, totalLines);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建空单元格样式(带边框)
|
||||
*/
|
||||
@ -2448,39 +2529,17 @@ public class BusProjectServiceImpl extends ServiceImpl<BusProjectMapper, BusProj
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置关键行的行高(调整行高设置,修正第24行高度)
|
||||
* 设置关键行的行高(移除固定行高设置,使用自动调整)
|
||||
*/
|
||||
private static void setKeyRowHeights(Sheet sheet) {
|
||||
setRowHeightSafe(sheet, 0, 25); // 标题行
|
||||
setRowHeightSafe(sheet, 1, 15); // 第2行(空行)
|
||||
setRowHeightSafe(sheet, 2, 20); // 项目信息行
|
||||
setRowHeightSafe(sheet, 3, 20); // 表头行
|
||||
setRowHeightSafe(sheet, 4, 30); // 项目基本信息行1
|
||||
setRowHeightSafe(sheet, 5, 30); // 项目基本信息行2
|
||||
setRowHeightSafe(sheet, 6, 60); // 本周进度行
|
||||
setRowHeightSafe(sheet, 7, 30); // 下周计划行
|
||||
setRowHeightSafe(sheet, 8, 45); // 质量情况行
|
||||
setRowHeightSafe(sheet, 9, 25); // 人员设备行
|
||||
|
||||
// 设置表格行高
|
||||
for (int i = 10; i <= 17; i++) { // 第11-18行(施工进度表)
|
||||
setRowHeightSafe(sheet, i, 20);
|
||||
// 不再设置固定行高,由autoAdjustRowHeights自动调整
|
||||
// 只设置最小行高确保基本显示
|
||||
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
|
||||
Row row = sheet.getRow(i);
|
||||
if (row != null && row.getHeightInPoints() < 20) {
|
||||
row.setHeightInPoints(20); // 设置最小行高
|
||||
}
|
||||
}
|
||||
for (int i = 18; i <= 22; i++) { // 第19-23行(设备材料表)
|
||||
setRowHeightSafe(sheet, i, 20);
|
||||
}
|
||||
|
||||
setRowHeightSafe(sheet, 23, 135); // 存在问题行(第24行)- 设置为原来的3倍(45*3=135)
|
||||
setRowHeightSafe(sheet, 24, 20); // 照片行
|
||||
setRowHeightSafe(sheet, 25, 20); // 填报审核行
|
||||
}
|
||||
|
||||
private static void setRowHeightSafe(Sheet sheet, int rowIndex, float height) {
|
||||
Row row = sheet.getRow(rowIndex);
|
||||
if (row == null) {
|
||||
row = sheet.createRow(rowIndex);
|
||||
}
|
||||
row.setHeightInPoints(height);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user