11-21-添加对应方法

This commit is contained in:
2025-11-21 11:15:40 +08:00
parent f6e1d0c73e
commit e6283bee1c

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.XSSFWorkbook; import org.apache.poi.xssf.usermodel.*;
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;
@ -1484,4 +1484,147 @@ 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());
}
}
} }