Java 如何获取cell';使用ApachePOI的背景色?
如何获得Java 如何获取cell';使用ApachePOI的背景色?,java,excel,apache-poi,Java,Excel,Apache Poi,如何获得XSSFCell的背景色。我尝试使用XSSFCellStyle,但没有成功 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).getCellS
XSSFCell
的背景色。我尝试使用XSSFCellStyle
,但没有成功
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
类型中获得背景色表示。我在scala中工作,但它是相同的。你的代码是对的
这是我的,看看你是否能找到不同点:
val wb = new XSSFWorkbook(path)
for (id <- 0.until(sheetTot)) {
val sh = wb.getSheetAt(id)
print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor())
}
val wb=新XSSF工作簿(路径)
对于(id签出此URL):
这将返回RGB代码,但不是精确的RGB代码。但与XLS自定义颜色选择器中的实际颜色代码相比,返回的颜色或多或少相同。请尝试以下操作:
row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()
请注意,Color
使用了两次以下是Scala中的内容,但它确实显示了如何从对象模型中获取颜色。我想从实际rgb值实例化一个java.awt.Color对象(这很有用,部分原因是当我在断点处停止时,调试器会为我显示对象的实际颜色,部分原因是这用于导出到与Excel无关的系统)。我忽略了颜色的alpha值,我的Scala可能有点幼稚。我建议,如果这对您不起作用,您应该设置一个断点,并检查密切相关的方法调用的结果,例如getFillBackgroundColorColor()
val rgb:Array[Byte]=cell.getCellStyle.getFillForegroundColorColor.getRgb
def toInt(b:字节):Int={
如果(b它总是给我64号
这是我的密码
for(Row r : my_sheet) {
for (Cell c : r) {
System.out.println(c.getCellStyle().getFillBackgroundColor() );
//if foreground filter color is not green then hide the record
if ( c.getColumnIndex()==1 && c.getCellStyle().getFillBackgroundColor() !=17){
r1=(XSSFRow) c.getRow();
if (r1.getRowNum()!=0) { /* Ignore top row */
/* Hide Row that does not meet Filter Criteria */
r1.getCTRow().setHidden(true); }
}
}
}
发布您尝试过的代码。@AAA:您的代码似乎是正确的…您在本例中得到的是…默认情况下,在我的一端显示为64…@Sankumarsing我也得到了64,这是自动颜色代码。当您的工作簿有一些不同的颜色时,它没有任何意义。但请尝试以下cell.getCellStyle().setFillForegroundColor(HSSFColor.GOLD.index);
System.out.println(cell.getCellStyle().getFillForegroundColor())
将为您提供51…表示它正在根据颜色工作和变化。@Sankumarsing我尝试了这个方法,我也可以获得除64以外的其他值,但如果我使用任何工具(而不是POI)在Excel中设置,我将无法获取背景色。我使用的Excel文件不是通过POI生成的,而是像Microsoft Excel这样的工具。如果u每次都会检查不同的背景色,它会给你值64,这是自动的
颜色是的,64指自动的。但是我们如何得到实际的颜色?对我来说也是如此。调色板指的是什么?当cellStyle.getFillForegroundColor()或cellStyle.getFillBackgroundColor()是否始终返回默认值?
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))
for(Row r : my_sheet) {
for (Cell c : r) {
System.out.println(c.getCellStyle().getFillBackgroundColor() );
//if foreground filter color is not green then hide the record
if ( c.getColumnIndex()==1 && c.getCellStyle().getFillBackgroundColor() !=17){
r1=(XSSFRow) c.getRow();
if (r1.getRowNum()!=0) { /* Ignore top row */
/* Hide Row that does not meet Filter Criteria */
r1.getCTRow().setHidden(true); }
}
}
}