[英]How to set a custom color to a HSSCell using POI?
I would like to set a custom color to a CellStyle
in POI, but it doesn't seem to be getting applied.我想在 POI 中为CellStyle
设置自定义颜色,但它似乎没有被应用。
I have the following code:我有以下代码:
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("Bank Statement");
HSSFPalette palette = workBook.getCustomPalette();
palette.setColorAtIndex((short) 1, (byte) 60, (byte) 120, (byte) 216);
HSSFColor color = palette.findSimilarColor(60, 120, 216);
CellStyle cellStyleHeader = workBook.createCellStyle();
cellStyleHeader.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyleHeader.setFillForegroundColor(color);
cellStyleHeader.setFont(createHeaderFont(workBook));
HSSFCell cell = row.createCell(cellIndex);
cell.setCellType(CellType.STRING);
cell.setCellValue("Date");
cell.setCellStyle(style);
No background color is being applied.没有应用背景颜色。 The color is white.颜色是白色的。 The value of color
is non null, but is not the same color (when run via a debugger): color
的值不是 null,但不是相同的颜色(通过调试器运行时):
What's the correct way to set a custom color?设置自定义颜色的正确方法是什么?
Seems void setFillForegroundColor(Color color) does not work properly for HSSFColor
.似乎void setFillForegroundColor(Color color)对于HSSFColor
不能正常工作。
In the binary Excel file format ( *.xls
, HSSF
) all colors need to be indexed in a color palette.在二进制 Excel 文件格式( *.xls
, HSSF
)中,所有 colors 都需要在调色板中进行索引。 So a custom color needs overwriting a default palette color.因此自定义颜色需要覆盖默认调色板颜色。 And to set the fill foreground color, the index should be used rather than the color itself.并且要设置填充前景色,应该使用索引而不是颜色本身。 So void setFillForegroundColor(short bg) should be used for HSSF
.所以void setFillForegroundColor(short bg)应该用于HSSF
。
Following code works to set custom fill foreground color for XSSF
as well as for HSSF
.以下代码用于为XSSF
和HSSF
设置自定义填充前景色。 For HSSF
it overwrites the HSSFColor.HSSFColorPredefined.LIME
color in palette.对于HSSF
,它会覆盖调色板中的HSSFColor.HSSFColorPredefined.LIME
颜色。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
public class CreateExcelCellFillCustomColor {
public static void main(String[] args) throws Exception {
byte[] rgb = new byte[]{(byte) 60, (byte) 120, (byte) 216};
Workbook workbook = new HSSFWorkbook(); String filePath = "./Excel.xls";
//Workbook workbook = new XSSFWorkbook(); String filePath = "./Excel.xlsx";
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
if (workbook instanceof XSSFWorkbook) {
XSSFColor color = new XSSFColor(rgb, null);
cellStyle.setFillForegroundColor(color);
} else if (workbook instanceof HSSFWorkbook) {
HSSFWorkbook hssfworkbook = (HSSFWorkbook)workbook;
HSSFPalette palette = hssfworkbook.getCustomPalette();
palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LIME.getIndex(), rgb[0], rgb[1], rgb[2]);
HSSFColor color = palette.findSimilarColor(rgb[0], rgb[1], rgb[2]);
//cellStyle.setFillForegroundColor(color); // does not work correctly for HSSF
cellStyle.setFillForegroundColor(color.getIndex());
}
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue("test");
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.