APACHE POI 4.1:从十六进制代码设置单元格背景颜色 [英] APACHE POI 4.1 : Set cell background color from hex code

查看:244
本文介绍了APACHE POI 4.1:从十六进制代码设置单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.

我正在做类似:

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.

对于 DefaultIndexedColorMap实例在调试结果中没有字段,我有些怀疑:

I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:

在这一点上,我不确定该怎么做解决。
其他帖子中的每个人似乎都可以正常工作,但我的背景仍然是深色而不是黄色。

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!

推荐答案

如其他答案所示, setFillForegroundColor(XSSFColor color)对于自定义颜色,在 XSSFCellStyle 中,必须使用而不是使用索引颜色。但是索引颜色的使用来自 org.apache.poi在 XSSF 中也可以使用.ss.usermodel.IndexedColors 。如果不需要使用自定义颜色,这将是最兼容的方式。

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. But usage of indexed colors from org.apache.poi.ss.usermodel.IndexedColors is possible in XSSF too. And this will be the most compatible way if using customized colors is not necessary.

而且还可以从中创建 XSSFColor 应该避免使用 java.awt.Color 。构造函数 XSSFColor(java.awt.Color clr,IndexedColorMap地图)标记为仅测试。并且 java.awt.Color 在某些情况下将不可用。

But also creating the XSSFColor from java.awt.Color should be avoided. The constructor XSSFColor(java.awt.Color clr, IndexedColorMap map) is marked "TEST ONLY". And java.awt.Color will not be available in some circumstances.

因此,如果需要设置单元格十六进制代码的背景色,并且十六进制代码在 String 中,然后在 org.apache.commons.codec.binary.Hex 可用于从该 String 中获取一个 byte [] 数组。 Apache通用编解码器已经是 apache poi 的依赖项之一。然后构造函数 XSSFColor(byte [] rgb,IndexedColorMap colorMap)可以使用。 IndexedColorMap 到目前为止没有使用。因此可以将其设置为 null 。如果 IndexedColorMap 以后得到使用,则必须对代码进行调整。

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. Apache commons codec is one of apache poi's dependencies already. Then constructor XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap has no usage until now. So it can be set null. If IndexedColorMap gets any usage later, then the code has to be adjusted anyway.

示例:

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

 }
}

这篇关于APACHE POI 4.1:从十六进制代码设置单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆