为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_笑_17 2024-01-05 14:56:23
我尝试了堆栈溢出上发布的不同解决方案,将背景颜色应用于 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 fileString pathFileExport = buildPathExportFile("test-export");FileOutputStream fileOut = new FileOutputStream(pathFileExport);workbook.write(fileOut);fileOut.close();//close workbookworkbook.close();return Paths.get(pathFileExport);我认为我的代码中一切正常,但每个像这样样式的单元格都会导致黑色背景。我对在没有字段的调试结果期间的“DefaultIndexedColorMap”实例有一些疑问:此时,我不确定要做什么来解决。其他帖子中的每个人似乎都能正常工作,但我仍然得到深色背景而不是黄色。有什么建议么?提前致谢!
查看完整描述

2 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

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

但也应该避免创建XSSFColorfrom 。java.awt.Color构造函数XSSFColor(java.awt.Color clr, IndexedColorMap map)被标记为“仅测试”。并且java.awt.Color在某些情况下将不可用。

因此,如果需要“从十六进制代码设置单元格背景颜色”并且十六进制代码位于 a 中Stringorg.apache.commons.codec.binary.Hex则可用于byte[]从中获取数组StringApache 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);

  }


 }

}


查看完整回答
反对 回复 2024-01-05
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

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

但是,您可以使用直接采用setFillForegroundColorXSSFColor.

cellStyle.setFillForegroundColor(bgColor);

当我使用此重载时,我会得到您期望的黄色作为背景。

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


查看完整回答
反对 回复 2024-01-05
  • 2 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信