我已经很清楚地发布了代码。
我想在excel中获取单元格的颜色,然后发布excel文件,只有绿色和黄色两种颜色。
如果我使用XSSFColor,则打印输出为XSSFColor @ 8b21b8fa和XSSFColor @ dfcdb1。因此,无论是绿色还是黄色,我都无法进行比较。
我希望将黄色打印为1,如果绿色打印为0。谢谢你的帮助!!!
for(Row row : sheet)
{
for(Cell cell : row)
{
switch(formulaEvaluator.evaluateInCell(cell).getCellType())
{
case Cell.CELL_TYPE_BLANK:
Color cellColor= cell.getCellStyle().getFillForegroundColorColor();
if(cellColor==Color.GREEN)
{
System.out.print(0+",");
}
else if(cellColor==Color.YELLOW)
{
System.out.print(1+",");
}
}
}
System.out.println();
}
this is my excel file shows a madarin character
最佳答案
虽然可以从XSSFColor
创建java.awt.Color
,但没有简单的可能性从java.awt.Color
获取XSSFColor
。
我们可以将单元格中ARGBHex
的XSSFColor
与从ARGBHex
中新创建的XSSFColor
的java.awt.Color
进行比较。
例:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFColor;
class ColorTest {
public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("ColorTest.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for(Cell cell : row) {
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
Color cellColor= cell.getCellStyle().getFillForegroundColorColor();
if (cellColor instanceof XSSFColor) {
XSSFColor xssfCellColor = (XSSFColor) cellColor;
if(xssfCellColor.getARGBHex().equals(new XSSFColor(java.awt.Color.GREEN).getARGBHex())) {
System.out.print(0+",");
} else if(xssfCellColor.getARGBHex().equals(new XSSFColor(java.awt.Color.YELLOW).getARGBHex())) {
System.out.print(1+",");
}
}
break;
}
}
}
System.out.println();
} catch (InvalidFormatException ifex) {
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}
但是在提供的图片中,RGB 00FF00的绿色似乎并不是真正的绿色,而是混浊的绿色。因此,与
java.awt.Color.GREEN
的比较将不匹配,因为java.awt.Color.GREEN
正是RGB 00FF00。XSSF
和HSSF
的示例:import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.hssf.util.HSSFColor;
class ColorTest {
public static void main(String[] args) {
try {
//InputStream inp = new FileInputStream("ColorTest.xlsx");
InputStream inp = new FileInputStream("ColorTest.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for(Cell cell : row) {
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
Color cellColor= cell.getCellStyle().getFillForegroundColorColor();
if (cellColor instanceof XSSFColor) {
XSSFColor xssfCellColor = (XSSFColor) cellColor;
System.out.println(xssfCellColor.getARGBHex());
if(xssfCellColor.getARGBHex().equals(new XSSFColor(java.awt.Color.GREEN).getARGBHex())) {
System.out.println(cell.getAddress() + " is green");
} else if(xssfCellColor.getARGBHex().equals(new XSSFColor(java.awt.Color.YELLOW).getARGBHex())) {
System.out.println(cell.getAddress() + " is yellow");
}
} else if (cellColor instanceof HSSFColor) {
HSSFColor hssfCellColor = (HSSFColor) cellColor;
System.out.println(hssfCellColor.getHexString());
if(hssfCellColor.getHexString().equals("0:FFFF:0")) {
System.out.println(cell.getAddress() + " is green");
} else if(hssfCellColor.getHexString().equals("FFFF:FFFF:0")) {
System.out.println(cell.getAddress() + " is yellow");
}
}
break;
}
}
}
} catch (InvalidFormatException ifex) {
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}