Java 导出 Excel 时,如何设置条件格式数据条?

使用Java 导出一个Excel文件,如何给单元格设置条件格式数据条样式?

阅读 742
1 个回答

可以尝试GcExcel Java 组件,其支持条件格式的数据条,具体代码如下:
更多GcExcel支持的条件格式可以参考:Excel API Library for Java - Sample Browser | GrapeCity Documents | Conditional Formatting

public void ConditionFormatDataBar(){
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    Object data = new Object[][]{
            {"Name", "City", "Birthday", "Eye color", "Weight", "Height"},
            {"Richard", "New York", new GregorianCalendar(1968, 5, 8), "Blue", 67, 165},
            {"Nia", "New York", new GregorianCalendar(1972, 6, 3), "Brown", 62, 134},
            {"Jared", "New York", new GregorianCalendar(1964, 2, 2), "Hazel", 72, 180},
            {"Natalie", "Washington", new GregorianCalendar(1972, 7, 8), "Blue", 66, 163},
            {"Damon", "Washington", new GregorianCalendar(1986, 1, 2), "Hazel", 76, 176},
            {"Angela", "Washington", new GregorianCalendar(1993, 1, 15), "Brown", 68, 145}
    };
    worksheet.getRange("A1:F7").setValue(data);

    //添加数据条规则
    IDataBar dataBar = worksheet.getRange("E2:E7").getFormatConditions().addDatabar();

    dataBar.getMinPoint().setType(ConditionValueTypes.LowestValue);
    dataBar.getMinPoint().setValue(null);
    dataBar.getMaxPoint().setType(ConditionValueTypes.HighestValue);
    dataBar.getMaxPoint().setValue(null);

    dataBar.setBarFillType(DataBarFillType.Gradient);
    dataBar.getBarColor().setColor(Color.GetGreen());
    dataBar.setDirection(DataBarDirection.Context);
    dataBar.getAxisColor().setColor(Color.GetRed());
    dataBar.setAxisPosition(DataBarAxisPosition.Automatic);
    dataBar.getNegativeBarFormat().setBorderColorType(DataBarNegativeColorType.Color);
    dataBar.getNegativeBarFormat().getBorderColor().setColor(Color.GetBlue());
    dataBar.getNegativeBarFormat().setColorType(DataBarNegativeColorType.Color);
    dataBar.getNegativeBarFormat().getColor().setColor(Color.GetPink());
    dataBar.setShowValue(false);

    //保存为Excel文件
    workbook.save("output/CreateDatabBarRule.xlsx");
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏