FileInputStream in = new FileInputStream(new File("地址"));
//通过输入流读取指定的Excel文件
XSSFWorkbook workbook = new XSSFWorkbook(in);
//获取Excel文件的第1个Sheet页
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
//这里获取的是文本类型的单元格格式其他类型比如NUMERIC BOOLEAN FORMULA
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
XSSFFont fontAt =
workbook.getFontAt(cell.getCellStyle().getFontIndex());
XSSFColor xssfColor = fontAt.getXSSFColor();
if (xssfColor != null) {
byte[] rgb = xssfColor.getRGB();
int r = (rgb[0] < 0 ? rgb[0] + 256 : rgb[0]);
int g = (rgb[1] < 0 ? rgb[1] + 256 : rgb[1]);
int b = (rgb[2] < 0 ? rgb[2] + 256 : rgb[2]);
System.out.println("颜色:"+ r + ", " + g + ", " + b);
} else {
System.out.println("Font color: default");
}
System.out.println("----------------------------------------");
break;
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
这是我使用的依赖都是目前最新版本的!
谢谢支持!