11-21-还原

This commit is contained in:
2025-11-21 16:11:42 +08:00
parent b520ea6afe
commit 3f21d39653

View File

@ -12,7 +12,7 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dromara.common.core.constant.HttpStatus; import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ObjectUtils; import org.dromara.common.core.utils.ObjectUtils;
@ -471,7 +471,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
/** /**
* 构建模板完整结构(添加自动调整行高) * 构建模板完整结构(添加自动调整行高)
*/ */
private void buildTemplateFullStructure(Sheet sheet, Workbook workbook,HseSafetyWeeklyReport report,Integer weekOfYear) { private void buildTemplateFullStructure(Sheet sheet, Workbook workbook,HseSafetyWeeklyReport report,Integer weekOfYear) {
// 创建所有需要的样式 // 创建所有需要的样式
CellStyle titleStyle = createTitleCellStyle(workbook); CellStyle titleStyle = createTitleCellStyle(workbook);
CellStyle headerStyle = createHeaderCellStyle(workbook); CellStyle headerStyle = createHeaderCellStyle(workbook);
@ -526,10 +526,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
// 15. 确保所有单元格都有边框 // 15. 确保所有单元格都有边框
ensureAllCellsHaveBorders(sheet, workbook); ensureAllCellsHaveBorders(sheet, workbook);
// 16. 为施工进度和设备材料表格的I列添加条件格式化数据条 // 16. 自动调整行高以适应内容
addPercentageFillForProgressTable(sheet, workbook);
// 17. 自动调整行高以适应内容
autoAdjustRowHeights(sheet); autoAdjustRowHeights(sheet);
} }
@ -639,7 +636,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 项目基本信息模块第5-6行- 修正合并区域 * 项目基本信息模块第5-6行- 修正合并区域
*/ */
private void createProjectBasicInfoModule(Sheet sheet, Workbook workbook, int startRow, private void createProjectBasicInfoModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
BusProjectVo busProjectVo,HseSafetyWeeklyReport report) { BusProjectVo busProjectVo,HseSafetyWeeklyReport report) {
Row row1 = sheet.createRow(startRow); Row row1 = sheet.createRow(startRow);
Row row2 = sheet.createRow(startRow + 1); Row row2 = sheet.createRow(startRow + 1);
@ -694,7 +691,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 本周进度模块第7行 * 本周进度模块第7行
*/ */
private void createWeeklyProgressModule(Sheet sheet, Workbook workbook, int startRow, private void createWeeklyProgressModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
HseSafetyWeeklyReport report) { HseSafetyWeeklyReport report) {
Row progressRow = sheet.createRow(startRow); Row progressRow = sheet.createRow(startRow);
progressRow.setHeightInPoints(60); progressRow.setHeightInPoints(60);
@ -762,7 +759,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 修正其他模块的行号(去掉空行后) * 修正其他模块的行号(去掉空行后)
*/ */
private void createNextWeekPlanModule(Sheet sheet, Workbook workbook, int startRow, private void createNextWeekPlanModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
HseSafetyWeeklyReport report) { HseSafetyWeeklyReport report) {
Row planRow = sheet.createRow(startRow); Row planRow = sheet.createRow(startRow);
@ -824,7 +821,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
} }
private void createQualityStatusModule(Sheet sheet, Workbook workbook, int startRow, private void createQualityStatusModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle) { CellStyle headerStyle, CellStyle contentStyle) {
Row qualityRow = sheet.createRow(startRow); Row qualityRow = sheet.createRow(startRow);
Cell seqCell = qualityRow.createCell(0); Cell seqCell = qualityRow.createCell(0);
@ -848,7 +845,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 人员设备模块第10行- 恢复原来的合并范围 * 人员设备模块第10行- 恢复原来的合并范围
*/ */
private void createPersonnelEquipmentModule(Sheet sheet, Workbook workbook, int startRow, private void createPersonnelEquipmentModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
HseSafetyWeeklyReport report) { HseSafetyWeeklyReport report) {
Row personnelRow = sheet.createRow(startRow); Row personnelRow = sheet.createRow(startRow);
personnelRow.setHeightInPoints(25); personnelRow.setHeightInPoints(25);
@ -893,7 +890,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
} }
/** /**
* 施工进度统计表第11-18行- 修正数据赋值问题确保I列是数值格式 * 施工进度统计表第11-18行- 修正数据赋值问题
*/ */
private void createConstructionProgressTable(Sheet sheet, Workbook workbook, int startRow, private void createConstructionProgressTable(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
@ -937,18 +934,15 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
// 数据行第12-18行共7行- 修正数据位置和赋值 // 数据行第12-18行共7行- 修正数据位置和赋值
String[][] progressData = { String[][] progressData = {
{"基础", "光伏区", "", "164728", "3955", "104434", "63.4", "6000", "设计值为根据7.18日最新电子版图纸统计包含未开工的五工区总设计容量约为357.5Mpw。"}, {"基础", "光伏区", "", "164728", "3955", "104434", "63.4%", "6000", "设计值为根据7.18日最新电子版图纸统计包含未开工的五工区总设计容量约为357.5Mpw。"},
{"支架", "", "", "20591", "644", "6412", "31.1", "1050", ""}, {"支架", "", "", "20591", "644", "6412", "31.1%", "1050", ""},
{"组件", "", "", "20591", "409", "4902", "23.8", "850", ""}, {"组件", "", "", "20591", "409", "4902", "23.8%", "850", ""},
{"清表", "", "", "4517", "0", "4104", "90.8", "100", ""}, {"清表", "", "", "4517", "0", "4104", "90.8%", "100", ""},
{"塔基", "线路工程", "", "", "", "", "", "", ""}, {"塔基", "线路工程", "", "", "", "", "", "", ""},
{"组塔", "", "", "", "", "", "", "", ""}, {"组塔", "", "", "", "", "", "", "", ""},
{"放线", "", "", "", "", "", "", "", ""} {"放线", "", "", "", "", "", "", "", ""}
}; };
// 创建数值格式样式
CellStyle numberStyle = createNumberCellStyle(workbook);
for (int i = 0; i < progressData.length; i++) { for (int i = 0; i < progressData.length; i++) {
Row row = sheet.createRow(startRow + 1 + i); Row row = sheet.createRow(startRow + 1 + i);
row.setHeightInPoints(20); row.setHeightInPoints(20);
@ -959,22 +953,9 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
for (int j = 0; j < 7; j++) { // 只设置前7列数据C-I列 for (int j = 0; j < 7; j++) { // 只设置前7列数据C-I列
Cell cell = row.createCell(j + 2); // C列开始 Cell cell = row.createCell(j + 2); // C列开始
if (j < progressData[i].length) { if (j < progressData[i].length) {
if (j == 6) { // I列累计完成率- 设置为数值 cell.setCellValue(progressData[i][j]);
try {
double value = Double.parseDouble(progressData[i][j]);
cell.setCellValue(value);
cell.setCellStyle(numberStyle);
} catch (NumberFormatException e) {
cell.setCellValue(progressData[i][j]);
cell.setCellStyle(contentStyle);
}
} else {
cell.setCellValue(progressData[i][j]);
cell.setCellStyle(contentStyle);
}
} else {
cell.setCellStyle(contentStyle);
} }
cell.setCellStyle(contentStyle);
} }
// 设置J,K列下周计划完成- 使用数据数组的第8个元素 // 设置J,K列下周计划完成- 使用数据数组的第8个元素
@ -1011,7 +992,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
} }
/** /**
* 设备材料表格第19-23行- 修正数据赋值问题确保I列是数值格式 * 设备材料表格第19-23行- 修正数据赋值问题
*/ */
private void createEquipmentMaterialTable(Sheet sheet, Workbook workbook, int startRow, private void createEquipmentMaterialTable(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle, CellStyle headerStyle, CellStyle contentStyle,
@ -1055,15 +1036,12 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
// 数据行 - 从第20行开始共4行数据- 修正数据位置和赋值 // 数据行 - 从第20行开始共4行数据- 修正数据位置和赋值
String[][] materialData = { String[][] materialData = {
{"檩条、斜撑", "", "", "131728", "3752", "106745", "81.0", "", ""}, {"檩条、斜撑", "", "", "131728", "3752", "106745", "81.0%", "", ""},
{"立杆", "", "", "133841", "5549", "53372", "39.9", "", ""}, {"立杆", "", "", "133841", "5549", "53372", "39.9%", "", ""},
{"光伏组件", "", "", "576548", "25880", "177360", "30.8", "", ""}, {"光伏组件", "", "", "576548", "25880", "177360", "30.8%", "", ""},
{"预埋件", "", "", "164728", "0", "113000", "68.6", "", ""} {"预埋件", "", "", "164728", "0", "113000", "68.6%", "", ""}
}; };
// 创建数值格式样式
CellStyle numberStyle = createNumberCellStyle(workbook);
for (int i = 0; i < materialData.length; i++) { for (int i = 0; i < materialData.length; i++) {
Row row = sheet.createRow(startRow + 1 + i); Row row = sheet.createRow(startRow + 1 + i);
row.setHeightInPoints(20); row.setHeightInPoints(20);
@ -1074,22 +1052,9 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
for (int j = 0; j < 7; j++) { // 只设置前7列数据C-I列 for (int j = 0; j < 7; j++) { // 只设置前7列数据C-I列
Cell cell = row.createCell(j + 2); // C列开始 Cell cell = row.createCell(j + 2); // C列开始
if (j < materialData[i].length) { if (j < materialData[i].length) {
if (j == 6) { // I列累计到货率- 设置为数值 cell.setCellValue(materialData[i][j]);
try {
double value = Double.parseDouble(materialData[i][j]);
cell.setCellValue(value);
cell.setCellStyle(numberStyle);
} catch (NumberFormatException e) {
cell.setCellValue(materialData[i][j]);
cell.setCellStyle(contentStyle);
}
} else {
cell.setCellValue(materialData[i][j]);
cell.setCellStyle(contentStyle);
}
} else {
cell.setCellStyle(contentStyle);
} }
cell.setCellStyle(contentStyle);
} }
// 设置J,K列计划到货日期- 使用数据数组的第8个元素 // 设置J,K列计划到货日期- 使用数据数组的第8个元素
@ -1125,7 +1090,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 修正其他相关模块的行号引用 * 修正其他相关模块的行号引用
*/ */
private void createProblemsModule(Sheet sheet, Workbook workbook, int startRow, private void createProblemsModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle) { CellStyle headerStyle, CellStyle contentStyle) {
Row problemsRow = sheet.createRow(startRow); Row problemsRow = sheet.createRow(startRow);
Cell seqCell = problemsRow.createCell(0); Cell seqCell = problemsRow.createCell(0);
@ -1149,7 +1114,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 照片模块第26行- 修正序号 * 照片模块第26行- 修正序号
*/ */
private void createPhotoModule(Sheet sheet, Workbook workbook, int startRow, private void createPhotoModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle) { CellStyle headerStyle, CellStyle contentStyle) {
Row photoRow = sheet.createRow(startRow); Row photoRow = sheet.createRow(startRow);
Cell seqCell = photoRow.createCell(0); Cell seqCell = photoRow.createCell(0);
@ -1169,7 +1134,7 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
* 填报审核模块第26行- 合并F-L列 * 填报审核模块第26行- 合并F-L列
*/ */
private void createApprovalModule(Sheet sheet, Workbook workbook, int startRow, private void createApprovalModule(Sheet sheet, Workbook workbook, int startRow,
CellStyle headerStyle, CellStyle contentStyle) { CellStyle headerStyle, CellStyle contentStyle) {
Row approvalRow = sheet.createRow(startRow); Row approvalRow = sheet.createRow(startRow);
Cell seqCell = approvalRow.createCell(0); Cell seqCell = approvalRow.createCell(0);
@ -1519,170 +1484,4 @@ public class HseSafetyWeeklyReportServiceImpl extends ServiceImpl<HseSafetyWeekl
} }
} }
/**
* 为施工进度统计表第12-18行和设备材料表格第20-23行的I列添加条件格式化数据条
*/
private void addPercentageFillForProgressTable(Sheet sheet, Workbook workbook) {
if (sheet instanceof XSSFSheet) {
// 使用XSSF的条件格式化.xlsx格式
addDataBarsConditionalFormatting((XSSFSheet) sheet);
} else {
// 使用HSSF的条件格式化.xls格式
addColorScaleConditionalFormatting(sheet, workbook);
}
}
/**
* 为XSSFSheet添加数据条条件格式化.xlsx格式
*/
private void addDataBarsConditionalFormatting(XSSFSheet sheet) {
try {
XSSFSheetConditionalFormatting conditionalFormatting = sheet.getSheetConditionalFormatting();
// 施工进度统计表I12-I18
CellRangeAddress[] progressRanges = new CellRangeAddress[] {
new CellRangeAddress(11, 17, 8, 8) // I12-I18
};
// 设备材料表格I20-I23
CellRangeAddress[] materialRanges = new CellRangeAddress[] {
new CellRangeAddress(19, 22, 8, 8) // I20-I23
};
// 创建数据条规则 - 使用正确的API
XSSFConditionalFormattingRule dataBarRule = conditionalFormatting.createConditionalFormattingRule(
org.apache.poi.ss.usermodel.ComparisonOperator.NO_COMPARISON,
null,
null
);
// 获取数据条格式化对象并设置颜色
XSSFDataBarFormatting dataBarFormatting = dataBarRule.getDataBarFormatting();
// 设置数据条颜色为红色
XSSFColor redColor = new XSSFColor(new java.awt.Color(255, 0, 0), new DefaultIndexedColorMap());
dataBarFormatting.setColor(redColor);
// 设置最小值和最大值(自动检测)
dataBarFormatting.getMinThreshold().setRangeType(org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType.MIN);
dataBarFormatting.getMaxThreshold().setRangeType(org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType.MAX);
// 应用数据条规则到施工进度表
conditionalFormatting.addConditionalFormatting(progressRanges, dataBarRule);
// 应用数据条规则到设备材料表
conditionalFormatting.addConditionalFormatting(materialRanges, dataBarRule);
System.out.println("数据条条件格式化已成功应用");
} catch (Exception e) {
System.err.println("创建数据条条件格式化失败: " + e.getMessage());
e.printStackTrace();
// 降级到颜色刻度方案
addColorScaleConditionalFormatting(sheet, sheet.getWorkbook());
}
}
/**
* 为HSSFSheet添加颜色刻度条件格式化.xls格式
*/
private void addColorScaleConditionalFormatting(Sheet sheet, Workbook workbook) {
try {
// 施工进度统计表I12-I18
CellRangeAddress progressRange = new CellRangeAddress(11, 17, 8, 8);
// 设备材料表格I20-I23
CellRangeAddress materialRange = new CellRangeAddress(19, 22, 8, 8);
// 创建条件格式化规则
SheetConditionalFormatting conditionalFormatting = sheet.getSheetConditionalFormatting();
// 创建颜色刻度规则(三色刻度:白-粉-红)
ConditionalFormattingRule rule = conditionalFormatting.createConditionalFormattingColorScaleRule();
// 应用规则
ConditionalFormattingRule[] rules = new ConditionalFormattingRule[]{rule};
CellRangeAddress[] ranges = new CellRangeAddress[]{progressRange, materialRange};
conditionalFormatting.addConditionalFormatting(ranges, rules);
} catch (Exception e) {
System.err.println("创建颜色刻度条件格式化失败: " + e.getMessage());
// 最终降级方案:使用简单的条件格式化
addSimpleConditionalFormatting(sheet, workbook);
}
}
/**
* 简单的条件格式化降级方案
*/
private void addSimpleConditionalFormatting(Sheet sheet, Workbook workbook) {
try {
SheetConditionalFormatting conditionalFormatting = sheet.getSheetConditionalFormatting();
// 定义条件格式化规则
// 规则1: 0-30% - 浅红色
ConditionalFormattingRule rule1 = conditionalFormatting.createConditionalFormattingRule(
"AND(ISNUMBER(I1), I1<=30)");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// 规则2: 31-60% - 中等红色
ConditionalFormattingRule rule2 = conditionalFormatting.createConditionalFormattingRule(
"AND(ISNUMBER(I1), I1>30, I1<=60)");
PatternFormatting fill2 = rule2.createPatternFormatting();
fill2.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// 规则3: 61-100% - 深红色
ConditionalFormattingRule rule3 = conditionalFormatting.createConditionalFormattingRule(
"AND(ISNUMBER(I1), I1>60)");
PatternFormatting fill3 = rule3.createPatternFormatting();
fill3.setFillBackgroundColor(IndexedColors.RED.getIndex());
fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// 应用规则到两个区域
ConditionalFormattingRule[] rules = new ConditionalFormattingRule[]{rule1, rule2, rule3};
// 施工进度统计表
CellRangeAddress[] progressRanges = new CellRangeAddress[] {
new CellRangeAddress(11, 17, 8, 8)
};
conditionalFormatting.addConditionalFormatting(progressRanges, rules);
// 设备材料表格
CellRangeAddress[] materialRanges = new CellRangeAddress[] {
new CellRangeAddress(19, 22, 8, 8)
};
conditionalFormatting.addConditionalFormatting(materialRanges, rules);
} catch (Exception e) {
System.err.println("创建简单条件格式化失败: " + e.getMessage());
}
}
/**
* 创建数值格式样式
*/
private CellStyle createNumberCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)10);
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
// 设置数值格式为百分比
DataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("0.0%"));
return style;
}
} }