简体   繁体   English

APACHE POI 4.1:从十六进制代码设置单元格背景颜色

[英]APACHE POI 4.1 : Set cell background color from hex code

I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.我尝试了在堆栈溢出上发布的不同解决方案,以将背景颜色应用于 Apache POI 生成的单元格,但没有任何效果。

I'm doing something like:我正在做类似的事情:

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);

I think everything is ok in my code but every cell styled like that will result in a black background.我认为我的代码中的一切都很好,但是每个像这样样式的单元格都会导致黑色背景。 黑细胞

I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:我对没有字段的调试结果期间的“DefaultIndexedColorMap”实例有一些疑问:

代码调试器

At this point, I'm not sure about what to do to solve.在这一点上,我不确定该怎么做才能解决。 Everyone in other posts seems to get things working but I'm still getting dark backgrounds instead of yellow.其他帖子中的每个人似乎都在工作,但我仍然得到深色背景而不是黄色。

Any suggestions?有什么建议么? Thanks in advance!提前致谢!

As the other answer tells, usage of setFillForegroundColor(XSSFColor color) instead of using indexed colors is necessary in XSSFCellStyle when it comes to customized colors.正如另一个答案所说,当涉及到自定义 colors 时,在XSSFCellStyle中需要使用setFillForegroundColor(XSSFColor color)而不是使用索引 colors。 But usage of indexed colors from org.apache.poi.ss.usermodel.IndexedColors is possible in XSSF too.但是在 XSSF 中也可以使用来自org.apache.poi.ss.usermodel.IndexedColors的索引XSSF And this will be the most compatible way if using customized colors is not necessary.如果不需要使用定制的 colors,这将是最兼容的方式。

But also creating the XSSFColor from java.awt.Color should be avoided.但也应避免从java.awt.Color创建XSSFColor The constructor XSSFColor(java.awt.Color clr, IndexedColorMap map) is marked "TEST ONLY".构造函数XSSFColor(java.awt.Color clr, IndexedColorMap map)被标记为“仅测试”。 And java.awt.Color will not be available in some circumstances.并且java.awt.Color在某些情况下将不可用。

So if the need is "set cell background color from hex code" and the hex code is in a String , then org.apache.commons.codec.binary.Hex can be used to get an byte[] array from that String .因此,如果需要“从十六进制代码设置单元格背景颜色”并且十六进制代码在String中,则org.apache.commons.codec.binary.Hex可用于从该String获取byte[]数组。 Apache commons codec is one of apache poi 's dependencies already. Apache commons codecapache poi的依赖项之一。 Then constructor XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used.然后可以使用构造函数XSSFColor(byte[] rgb, IndexedColorMap colorMap) IndexedColorMap has no usage until now. IndexedColorMap还没有使用。 So it can be set null .所以可以设置null If IndexedColorMap gets any usage later, then the code has to be adjusted anyway.如果IndexedColorMap得到任何使用,那么无论如何都必须调整代码。

Example:例子:

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);
  }

 }
}

I've noticed that when working with colors in xlsx files (XSSF), using indexed colors doesn't work very well.我注意到在 xlsx 文件 (XSSF) 中使用 colors 时,使用索引的 colors 效果不佳。 It appears that there is no index for any colors in an XSSFWorkbook by default, so you can't use an index of color that isn't indexed.默认情况下, XSSFWorkbook中似乎没有任何 colors 的索引,因此您不能使用未编入索引的颜色索引。

However, you can use the overload of setFillForegroundColor that directly takes an XSSFColor .但是,您可以使用直接采用XSSFColor setFillForegroundColor重载

cellStyle.setFillForegroundColor(bgColor);

When I use this overload, then I get the yellow color as a background that you're expecting.当我使用这个重载时,我会得到你所期望的黄色作为背景。

Generally when working with colors in XSSF you should use the XSSFColor itself and not its index.通常,在 XSSF 中使用 colors 时,您应该使用XSSFColor本身而不是其索引。 This goes for other things like the other patterned color ("background"), border colors, and font colors.这适用于其他事物,例如其他图案颜色(“背景”)、边框 colors 和字体 colors。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM