Cell25.4 怎么设置数据条和色阶

数据条的最大值和最小值怎么设置,通过setMaxLength和setMinLength?

另外色阶怎么设置excel常用的几个模版,能否提供下示例?

@YU_tou

原因

要设置数据条和色阶的最大值和最小值,可以使用 setMaxLengthsetMinLength 方法。数据条和色阶的配置可以通过相应的类来实现。

解决方案

  1. 设置数据条的最大值和最小值

    • 使用 DataBar 类来创建数据条,并通过 setMaxLengthsetMinLength 方法设置最大和最小长度。
  2. 设置色阶

    • 使用 ColorScale 类来创建色阶,并设置最大值、最小值和中间值的颜色。

代码示例

以下是如何设置数据条和色阶的示例代码:

// 创建工作簿对象
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

// 添加条件格式
int index = sheet.getConditionalFormattings().add();
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(index);

// 设置条件格式范围
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 2;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.addArea(ca);

// 添加数据条条件
int idx = fcs.addCondition(FormatConditionType.DATA_BAR);
FormatCondition cond = fcs.get(idx);
DataBar dataBar = cond.getDataBar();
dataBar.setColor(Color.getOrange());
dataBar.setMinLength(10); // 设置最小长度
dataBar.setMaxLength(100); // 设置最大长度

// 添加色阶条件
int colorScaleIdx = fcs.addCondition(FormatConditionType.COLOR_SCALE);
FormatCondition colorScaleCond = fcs.get(colorScaleIdx);
ColorScale colorScale = colorScaleCond.getColorScale();
colorScale.setMinColor(Color.getRed());
colorScale.setMaxColor(Color.getGreen());
colorScale.setMidColor(Color.getBlue()); // 如果需要三色阶

// 保存Excel文件
workbook.save("output.xlsx");

后续步骤

请根据您的需求调整颜色和长度设置。如果您有其他问题或需要进一步的帮助,请随时告诉我!我将很高兴为您提供支持。

我 需要设置类型
image.jpg (92.4 KB)

@YU_tou

关于如何设置色阶,请参考以下文档。

没有代码示例? 另外上面的数据条的值类型怎么设置呢

@YU_tou
可能由于网络问题,导致你不能查看样例代码。请参考以下样例代码设置色阶。

// Create workbook
Workbook workbook = new Workbook();

// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Add some data in cells
worksheet.getCells().get("A1").putValue("2-Color Scale");
worksheet.getCells().get("D1").putValue("3-Color Scale");

for (int i = 2; i <= 15; i++) {
	worksheet.getCells().get("A" + i).putValue(i);
	worksheet.getCells().get("D" + i).putValue(i);
}

// Adding 2-Color Scale Conditional Formatting
CellArea ca = CellArea.createCellArea("A2", "A15");

int idx = worksheet.getConditionalFormattings().add();
FormatConditionCollection fcc = worksheet.getConditionalFormattings().get(idx);
fcc.addCondition(FormatConditionType.COLOR_SCALE);
fcc.addArea(ca);

FormatCondition fc = worksheet.getConditionalFormattings().get(idx).get(0);
fc.getColorScale().setIs3ColorScale(false);
fc.getColorScale().setMaxColor(Color.getLightBlue());
fc.getColorScale().setMinColor(Color.getLightGreen());

// Adding 3-Color Scale Conditional Formatting
ca = CellArea.createCellArea("D2", "D15");

idx = worksheet.getConditionalFormattings().add();
fcc = worksheet.getConditionalFormattings().get(idx);
fcc.addCondition(FormatConditionType.COLOR_SCALE);
fcc.addArea(ca);

fc = worksheet.getConditionalFormattings().get(idx).get(0);
fc.getColorScale().setIs3ColorScale(true);
fc.getColorScale().setMaxColor(Color.getLightBlue());
fc.getColorScale().setMidColor(Color.getYellow());
fc.getColorScale().setMinColor(Color.getLightGreen());

// Save the workbook
workbook.save(dataDir + "output.xlsx");

@YU_tou
关于如何设置数据条的值类型和数据值,请参考以下样例代码。

// 创建工作簿对象
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.getCells().get("A1").putValue(20);
sheet.getCells().get("A2").putValue(50);
sheet.getCells().get("A3").putValue(80);


// 添加条件格式
int index = sheet.getConditionalFormattings().add();
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(index);

// 设置条件格式范围
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 2;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.addArea(ca);

// 添加数据条条件
int idx = fcs.addCondition(FormatConditionType.DATA_BAR);
FormatCondition cond = fcs.get(idx);
DataBar dataBar = cond.getDataBar();
dataBar.setColor(Color.getOrange());
dataBar.setMinLength(0); // 设置最小长度
dataBar.setMaxLength(100); // 设置最大长度

//设置最大最小值类型和数据值
ConditionalFormattingValue max = dataBar.getMaxCfvo();        
max.setType(FormatConditionValueType.NUMBER);
max.setValue(120);
ConditionalFormattingValue min = dataBar.getMinCfvo();
min.setType(FormatConditionValueType.NUMBER);
min.setValue(30);

// 保存Excel文件
workbook.save(filePath + "output_java.xlsx");