【问题标题】:Simple POI background fill issue简单的 POI 背景填充问题
【发布时间】:2016-06-29 19:47:52
【问题描述】:

我使用的是 poi 3.14,我想要的只是将单元格的背景设置为橙色。我不想要填充图案,只想要一个纯橙色,但我无法让它工作。这是我所拥有的:

    Row row = sheet.createRow(currentRow);

    CellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
    style.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(CellStyle.ALIGN_FILL);

    Cell cell = row.createCell(0);
    cell.setCellValue("ABCDEFG");
    cell.setCellStyle(style);

我最终得到的是带有small dots 图案的橙色背景。在 Excel 的属性表上,它说我有bg=orange, fg=automatic, pattern=25% Gray。如何使用简单的空白图案样式?

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:

    首先看起来不直观,但setFillForegroundColor 方法实际上设置了背景填充的前景色而不是文本(首先想到的是前景)。同样,您需要使用CellStyle.SOLID_FOREGROUND 填充图案。请参阅下面的工作版本。

    Row row = sheet.createRow(currentRow);
    
    CellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    
    Cell cell = row.createCell(0);
    cell.setCellValue("ABCDEFG");
    cell.setCellStyle(style);
    

    更多示例请参考 poi 用户指南:http://poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills

    【讨论】:

    • 谢谢,我不知道为什么我没有尝试这个组合,但是就像你说的那样,它不直观。我认为实施本质上是错误的(即相反)。因为它与 Excel 中显示的内容不匹配。文本一直是前景。根据所引用的示例,setFillPattern 的使用似乎有所不同,具体取决于设置的是背景还是前景。
    • @ergonaut 不,背景/前景在 POI 中没有搞砸。微软应该为这种奇怪的措辞负责:阴影可以由两种颜色(即“背景”“前景”)+一个索引不同模式的值组成。 Word 实现了基本相同的行为,请参阅here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2015-06-28
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多