【问题标题】:APACHE POI 4.1 : Set cell background color from hex codeAPACHE POI 4.1:从十六进制代码设置单元格背景颜色
【发布时间】:2019-10-28 16:33:07
【问题描述】:

我尝试了在堆栈溢出上发布的不同解决方案,以将背景颜色应用于 Apache POI 生成的单元格,但没有任何效果。

我正在做类似的事情:

Workbook workbook = new XSSFWorkbook(); 
Sheet sheet = workbook.createSheet(sheetName);

XSSFCellStyle cellStyle = ((XSSFCellStyle) workbook.createCellStyle());

if (styleObject.getBgColor() != null) {
    java.awt.Color javaBdgColor = java.awt.Color.decode(voceStyle.getBgColor()); // this is #FFF000
    XSSFColor bgColor = new XSSFColor(javaBdgColor, new DefaultIndexedColorMap());
    cellStyle.setFillForegroundColor(bgColor.getIndex());
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}

Row newRow = Rowsheet.createRow(0);
Cell newCell = newRow.createCell(0);
newCell.setCellStyle(cellStyle);

// write file
String pathFileExport = buildPathExportFile("test-export");
FileOutputStream fileOut = new FileOutputStream(pathFileExport);
workbook.write(fileOut);
fileOut.close();

//close workbook
workbook.close();

return Paths.get(pathFileExport);

我认为我的代码中一切正常,但每个像这样样式的单元格都会导致黑色背景。

我对没有字段的调试结果中的“DefaultIndexedColorMap”实例有一些疑问:

此时,我不确定如何解决。 其他帖子中的每个人似乎都能正常工作,但我的背景仍然是深色而不是黄色。

有什么建议吗? 提前致谢!

【问题讨论】:

    标签: java spring colors apache-poi apache-poi-4


    【解决方案1】:

    正如另一个答案所说,当涉及到自定义颜色时,XSSFCellStyle 中必须使用 setFillForegroundColor(XSSFColor color) 而不是使用索引颜色。但是在XSSF 中也可以使用来自org.apache.poi.ss.usermodel.IndexedColors 的索引颜色。如果不需要使用自定义颜色,这将是最兼容的方式。

    但也应避免从java.awt.Color 创建XSSFColor。构造函数XSSFColor(java.awt.Color clr, IndexedColorMap map) 被标记为“TEST ONLY”。而java.awt.Color 在某些情况下将不可用。

    因此,如果需要“从十六进制代码设置单元格背景颜色”并且十六进制代码在 String 中,则可以使用 org.apache.commons.codec.binary.Hex 从该 String 获取 byte[] 数组。 Apache commons codec 已经是 apache poi 的依赖项之一。然后可以使用构造函数XSSFColor(byte[] rgb, IndexedColorMap colorMap)IndexedColorMap 直到现在还没有使用。所以可以设置null。如果IndexedColorMap 以后有任何用途,那么无论如何都必须调整代码。

    例子:

    import java.io.FileOutputStream;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.*;
    
    import org.apache.commons.codec.binary.Hex;
    
    class CreateXSSFColor {
    
     public static void main(String[] args) throws Exception {
    
      try (Workbook workbook = new XSSFWorkbook(); 
           FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
    
       String rgbS = "FFF000";
       byte[] rgbB = Hex.decodeHex(rgbS); // get byte array from hex string
       XSSFColor color = new XSSFColor(rgbB, null); //IndexedColorMap has no usage until now. So it can be set null.
    
       XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
       cellStyle.setFillForegroundColor(color);
       cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    
       Sheet sheet = workbook.createSheet(); 
       Row row = sheet.createRow(0);
       Cell cell = row.createCell(0);
       cell.setCellValue("yellow");
       cell.setCellStyle(cellStyle);
    
       workbook.write(fileout);
      }
    
     }
    }
    

    【讨论】:

    • 像 rgettman 解决方案一样,即使我必须修复一些代码行,它也能完美运行。我实际上选择了这个作为解决方案,因为它更广泛,谈论代码可维护性。请注意,我已使用 String.replace("#", "") 从我的十六进制代码字符串“#FFF000”中删除了“#”字符
    【解决方案2】:

    我注意到在 xlsx 文件 (XSSF) 中使用颜色时,使用索引颜色效果不佳。默认情况下,XSSFWorkbook 中似乎没有任何颜色的索引,因此您不能使用未索引的颜色索引。

    但是,您可以使用overload of setFillForegroundColor that directly takes an XSSFColor

    cellStyle.setFillForegroundColor(bgColor);
    

    当我使用这个重载时,我会得到你所期望的黄色作为背景。

    通常在 XSSF 中使用颜色时,您应该使用 XSSFColor 本身而不是它的索引。这适用于其他事物,例如其他图案颜色(“背景”)、边框颜色和字体颜色。

    【讨论】:

    • 完美运行!非常感谢。是的,使用索引颜色不是很清楚,即使遵循 api 文档
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2011-09-06
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多