[英]How to convert XSSF color to Java.awt.color format. because XSSF colour could not make a compare
I have posted the code clealy . 我已经很清楚地发布了代码。 I want get the color of cell in excel, and i post my excel file and there are only two colors ,green and yellow.
我想在excel中获取单元格的颜色,然后发布excel文件,只有绿色和黄色两种颜色。 if I use XSSFColor ,the print out is XSSFColor@8b21b8fa and XSSFColor@dfcdb1.
如果我使用XSSFColor,则打印输出为XSSFColor @ 8b21b8fa和XSSFColor @ dfcdb1。 therefore, and I could not make a compare whether it is green or yellow.
因此,无论是绿色还是黄色,我都无法进行比较。 I hope to print out 1 if it is yellow, and 0 if it is green.
我希望将黄色打印为1,如果绿色打印为0。 thanks for any help!!!
谢谢你的帮助!!!
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 这是我的excel文件显示一个普通话字符
While it is possible to create a XSSFColor
from a java.awt.Color
, there is no simple possibility to get java.awt.Color
from a XSSFColor
. 虽然可以从
java.awt.Color
创建XSSFColor
,但没有简单的可能性从XSSFColor
获取java.awt.Color
。
We could compare the ARGBHex
of the XSSFColor
out of the cell with the ARGBHex
of a new created XSSFColor
from a java.awt.Color
. 我们可以将
ARGBHex
的XSSFColor
与该单元格中的ARGBHex
与来自java.awt.Color
的新创建的XSSFColor
进行比较。
Example: 例:
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) {
}
}
}
But in your provided picture the green color seems not to be really green with RGB 00FF00 but a muddy mixture green. 但是在提供的图片中,RGB 00FF00的绿色似乎并不是真正的绿色,而是混浊的绿色。 So the comparision with
java.awt.Color.GREEN
will not match, since java.awt.Color.GREEN
is exactly RGB 00FF00. 因此与比较
java.awt.Color.GREEN
将不匹配,因为java.awt.Color.GREEN
正是 RGB 00FF00。
Example for both XSSF
and HSSF
: 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) {
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.