Java 使用ApachePOI更改单元格颜色
我使用ApachePOI读取零件号电子表格中的数据。我在数据库中查找零件号,如果我们有零件的CAD图纸,我将零件号单元格涂成绿色,如果我们没有,我将其涂成红色。处理完成后,将保存电子表格。我遇到的问题是,该列中的每个单元格都是绿色的。我已经详细介绍了代码,查找零件号的逻辑工作正常,确定单元格应该是什么颜色以及设置颜色和填充的逻辑似乎也工作正常。知道我做错了什么吗 谢谢Java 使用ApachePOI更改单元格颜色,java,excel,apache-poi,Java,Excel,Apache Poi,我使用ApachePOI读取零件号电子表格中的数据。我在数据库中查找零件号,如果我们有零件的CAD图纸,我将零件号单元格涂成绿色,如果我们没有,我将其涂成红色。处理完成后,将保存电子表格。我遇到的问题是,该列中的每个单元格都是绿色的。我已经详细介绍了代码,查找零件号的逻辑工作正常,确定单元格应该是什么颜色以及设置颜色和填充的逻辑似乎也工作正常。知道我做错了什么吗 谢谢 //Check the parts for(int r=1;r<sheet.getPhysicalNumberOfRows
//Check the parts
for(int r=1;r<sheet.getPhysicalNumberOfRows();r++) {
String partNumber = null;
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
long pNum = (long) cell.getNumericCellValue();
partNumber = String.valueOf(pNum);
break;
case HSSFCell.CELL_TYPE_STRING:
partNumber = cell.getStringCellValue();
break;
default:
logger.info("Part Number at row " + r + " on sheet " + partList.getSheetName(s) + "is of an unsupported type");
}
try {
List<String> oldMaterialNumbers = getOldMaterialNumbers(partNumber);
boolean gotDrawing = checkPartNumber(oldMaterialNumbers, partNumber);
//If there's a drawing then color the row green, if not red.
short bgColorIndex = gotDrawing
?HSSFColor.LIGHT_GREEN.index //42
:HSSFColor.RED.index; //10
HSSFCell curCell = row.getCell(partNumberColumn);
HSSFCellStyle curStyle = curCell.getCellStyle();
curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
curStyle.setFillForegroundColor(bgColorIndex);
curCell.setCellStyle(curStyle);
}catch(Exception e) {
throw e;
}
}
//检查部件
对于(int r=1;r短版本:只创建一次样式,在任何地方都使用它们
长版本:使用方法创建所需的样式(注意样式数量的限制)
然后,在“主”代码中,从您拥有的样式映射中设置样式
Cell cell = xssfCurrentRow.createCell( intCellPosition );
cell.setCellValue( blah );
cell.setCellStyle( (CellStyle) styles.get("style1") );
我相信这是因为cell.getCellStyle
最初返回默认的单元格样式,然后更改
创建如下样式并将其应用于单元格:
cellStyle = (XSSFCellStyle) cell.getSheet().getWorkbook().createCellStyle();
尽管如前一张海报所述,尝试创建样式并重用它们
XSSF库中还有一些实用程序类,可以避免使用我提供的代码,并自动尝试和重用样式。现在记不起类了。要创建单元格样式,请参阅:
定制颜色
HSSF:
XSSF:
在这里签出示例
对于apache POI 3.9,您可以使用以下代码:
HSSFCellStyle style = workbook.createCellStyle()
style.setFillForegroundColor(HSSFColor.YELLOW.index)
style.setFillPattern((short) FillPatternType.SOLID_FOREGROUND.ordinal())
3.9版的方法接受short,您应该注意输入。确定checkPartNumber的输出吗?checkPartNumber的返回值是布尔值,并且该值返回正确。如果我这样做,则不获取单元格样式:CellStyle myStyle=partList.createCellStyle();我得到了正确的颜色,但丢失了现有的单元格格式。
cellStyle = (XSSFCellStyle) cell.getSheet().getWorkbook().createCellStyle();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
//replacing lime with freebsd.org gold
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell( 0);
cell.setCellValue("custom XSSF colors");
XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
HSSFCellStyle style = workbook.createCellStyle()
style.setFillForegroundColor(HSSFColor.YELLOW.index)
style.setFillPattern((short) FillPatternType.SOLID_FOREGROUND.ordinal())