2013-10-21 177 views
4

我们如何得到的XSSFCell。我尝试使用XSSFCellStyle,但没有运气。如何使用apache poi获取单元格的背景颜色?

FileInputStream fis = new FileInputStream(fileName); 
XSSFWorkbook book = new XSSFWorkbook(fis); 
XSSFSheet sheet = book.getSheetAt(0); 
XSSFRow row = sheet.getRow(0); 

System.out.println(row.getCell(0).getCellStyle().getFillForegroundColor()); 

使用这些步骤我无法获得Short类型的背景颜色表示。

+0

安置自己尝试代码的结果。 – Woody

+1

@AAA:你的代码似乎是正确的...你在这种情况下获得的...默认情况下,它显示64在我的末端... – Sankumarsingh

+0

@Sankumarsingh我也越来越64这是自动色彩代码它不会使任何意义,而你的工作簿有不同的颜色。 – AAA

回答

0

我在斯卡拉工作,但它是一样的。你的代码是正确的。

这是我的,看你能不能找到差异:

val wb = new XSSFWorkbook(path) 
for (id <- 0.until(sheetTot)) { 
    val sh = wb.getSheetAt(id)  
    print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor()) 
} 

在我的情况下,结果是64

+0

如果你每次检查和不同的背景颜色,它会给你值64这是'自动'颜色 – AAA

2

结帐这个网址:

https://issues.apache.org/bugzilla/show_bug.cgi?id=45492

Cell cell = row.getCell(1); 
      CellStyle cellStyle = cell.getCellStyle();   
      System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor())); 




private short[] getColorPattern(short colorIdx){   
    short[] triplet = null; 
    HSSFColor color = palette.getColor(colorIdx); 
    triplet = color.getTriplet();  
    System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]); 
    return triplet; 
} 

这返回RGB代码但不是确切的代码。但与XLS自定义颜色选择器中的实际颜色代码相比,其返回的颜色差不多相同。

1

试试这个:

row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex() 

注意Color使用了两次

0

以下是Scala但它确实表明究竟如何从对象模型的颜色。我想从实际的rgb值实例化一个java.awt.Color对象(这很有用,部分原因是我的调试器在停止断点时显示对象的实际颜色,部分原因是因为这是用于导出到具有与Excel无关)。我忽略了颜色的alpha值,我的Scala可能有点天真。我建议,如果这个不适合你,你应该设置一个断点,并检查紧密相关的方法调用,如getFillBackgroundColorColor()

val rgb: Array[Byte] = cell.getCellStyle.getFillForegroundColorColor.getRgb 
def toInt(b: Byte): Int = { 
    if (b<0) 256+b else b 
} 
val rgbInts = rgb.map(toInt) 
val color = new Color(rgbInts(0),rgbInts(1),rgbInts(2)) 
相关问题