使用Apache POI获取Cell Color

时间:2022-03-15 20:22:37

I'm trying to learn how to use Apache POI with a small project. I want to use Excel to create 'room layouts' by using colour-coded cells, and load the data into a Java program. I think understand how to access the colour properties of a cell, but what I'm asking is:

我正在尝试学习如何在一个小项目中使用Apache POI。我想使用Excel通过使用颜色编码的单元格来创建“房间布局”,并将数据加载到Java程序中。我想了解如何访问单元格的颜色属性,但我要问的是:

Is it possible to access the colour of a blank cell (no data or value), or does a cell need to have data in order for Apache POI to read it?

是否可以访问空白单元格的颜色(没有数据或值),或者单元格是否需要有数据才能让Apache POI读取它?

I am only interested in the colour, so might it be preferable to put junk data in the cells, or possibly iterate through them based on coordinates? I'm brand new to Apache POI, so any help is greatly appreciated.

我只对颜色感兴趣,所以最好把垃圾数据放在单元格中,还是可以根据坐标迭代它们?我是Apache POI的新手,所以非常感谢任何帮助。

1 个解决方案

#1


3  

What have you tried? Please read Busy Developers' Guide to HSSF and XSSF Features.

你尝试过什么?请阅读繁忙的开发人员HSSF和XSSF功能指南。

Supposing following Workbook:

假设以下工作簿:

使用Apache POI获取Cell Color

Then the following code should work as well with a.xls (HSSF) as with a.xlsx (XSSF).

然后,下面的代码也应该与a.xls(HSSF)和a.xlsx(XSSF)一样工作。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.*;

class ReadExcelEmptyColoredCells {

 public static void main(String[] args) {
  try {

   //Workbook workbook = WorkbookFactory.create(new File("a.xls"));
   Workbook workbook = WorkbookFactory.create(new File("a.xlsx"));

   Sheet sheet = workbook.getSheetAt(0);

   for (Row row : sheet) {
    for (Cell cell : row) {
     if (! "".equals(String.valueOf(cell)))
      System.out.println(cell.getAddress() + ": " + String.valueOf(cell));
     CellStyle cellStyle = cell.getCellStyle();
     Color color = cellStyle.getFillForegroundColorColor();
     if (color != null) {
      if (color instanceof XSSFColor) {
       System.out.println(cell.getAddress() + ": " + ((XSSFColor)color).getARGBHex());
      } else if (color instanceof HSSFColor) {
       if (! (color instanceof HSSFColor.AUTOMATIC))
        System.out.println(cell.getAddress() + ": " + ((HSSFColor)color).getHexString());
      }
     }
    }
   }

   workbook.close();

  } catch (InvalidFormatException ifex) {
  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }
 }
}

#1


3  

What have you tried? Please read Busy Developers' Guide to HSSF and XSSF Features.

你尝试过什么?请阅读繁忙的开发人员HSSF和XSSF功能指南。

Supposing following Workbook:

假设以下工作簿:

使用Apache POI获取Cell Color

Then the following code should work as well with a.xls (HSSF) as with a.xlsx (XSSF).

然后,下面的代码也应该与a.xls(HSSF)和a.xlsx(XSSF)一样工作。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.*;

class ReadExcelEmptyColoredCells {

 public static void main(String[] args) {
  try {

   //Workbook workbook = WorkbookFactory.create(new File("a.xls"));
   Workbook workbook = WorkbookFactory.create(new File("a.xlsx"));

   Sheet sheet = workbook.getSheetAt(0);

   for (Row row : sheet) {
    for (Cell cell : row) {
     if (! "".equals(String.valueOf(cell)))
      System.out.println(cell.getAddress() + ": " + String.valueOf(cell));
     CellStyle cellStyle = cell.getCellStyle();
     Color color = cellStyle.getFillForegroundColorColor();
     if (color != null) {
      if (color instanceof XSSFColor) {
       System.out.println(cell.getAddress() + ": " + ((XSSFColor)color).getARGBHex());
      } else if (color instanceof HSSFColor) {
       if (! (color instanceof HSSFColor.AUTOMATIC))
        System.out.println(cell.getAddress() + ": " + ((HSSFColor)color).getHexString());
      }
     }
    }
   }

   workbook.close();

  } catch (InvalidFormatException ifex) {
  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }
 }
}