0
import org.apache.poi.ss.usermodel
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


// Assume this method is wrapped in a class
public static void parseFile(){
    // File Path has a .xls extension
    FileInputStream file = new FileInputStream(filePath);
    HSSFWorkbook wb = new HSSFWorkbook(file);
    HSSFSheet sheet = wb.getSheetAt(0);

    for(int rn = sheet.getFirstRowNum(); rn <= sheet.getLastRowNum(); rn++){
        HSSFRow row = sheet.getRow(rn);

        for(int cn = 0; cn < row.getLastCellNum(); cn++){
            HSSFCell cell = row.getCell(cn);
            //This is apache's color
            HSSFColor color = cell.getCellStyle().getFillBackgroundColorColor; 

            // Excel entire row is red and it is not entering here.
            if(color.equals(IndexedColors.RED)){
                System.out.println("I made it here!");
                cn++;
                continue;
            }
        }
    }
}

该文件处理得很好,但它不会进入我的“if 语句”。我做错了吗?这可以用 xls 文件完成吗?我看到了 xlsx 的示例,但我没有看到 xls 的任何示例。谢谢!

4

1 回答 1

1

该方法getFillBackgroundColorColor()返回以下类型:

org.apache.poi.ss.usermodel.Color

不是

java.awt.Color

检查 RED 的一种方法是检查 RGB 值,如下所示:

byte[] rgb=colore.getRGB();
System.out.println("color" + " " + rgb[0] + " " + rgb[1] + " " + rgb[2] );
if ( (rgb[0] == (byte)0xff) 
  && (rgb[1] == 0)
  && (rgb[2] == 0)) {
     System.out.println("that is totally red");
}

重点是,不要将ColorapachePOI 类与 java.awt.Color

于 2018-01-28T10:27:54.543 回答