本文主要是介绍Java 查看Excel单元格背景填充色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
依赖
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version>
</dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version>
</dependency>
代码:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;public class ExcelTest3 {public static void main(String[] args) {ExcelTest3 excelTest3 = new ExcelTest3();Workbook workbook = excelTest3.getExcel("C:\\Users\\beawan\\Desktop\\test\\test.xlsx");excelTest3.analyzeExcel(workbook);}public Workbook getExcel(String filePath){Workbook wb=null;File file=new File(filePath);if(!file.exists()){System.out.println("文件不存在");wb=null;}else {String fileType=filePath.substring(filePath.lastIndexOf("."));//获得后缀名try {InputStream is = new FileInputStream(filePath);if(".xls".equals(fileType)){wb = new HSSFWorkbook(is);}else if(".xlsx".equals(fileType)){wb = new XSSFWorkbook(is);}else{System.out.println("格式不正确");wb=null;}}catch (Exception e){e.printStackTrace();}}return wb;}public void analyzeExcel(Workbook wb){Sheet sheet=wb.getSheetAt(0);//读取sheet(从0计数)int rowNum=sheet.getLastRowNum();//读取行数(从0计数)for(int i=0;i<=rowNum;i++){Row row=sheet.getRow(i);//获得行int colNum=row.getLastCellNum();//获得当前行的列数for(int j=0;j<colNum;j++){Cell cell=row.getCell(j);//获取单元格CellStyle cellStyle = cell.getCellStyle();// xls 03版
// HSSFColor hssfColor = (HSSFColor) cellStyle.getFillForegroundColorColor();
// String color = hssfColor.getHexString();
// System.out.println(color);// xlsx 07版XSSFColor xssfColor = (XSSFColor) cellStyle.getFillForegroundColorColor();byte[] bytes = xssfColor.getRGB();for (int k = 0; k < bytes.length; k++) {System.out.print(bytes[k] + "-");}System.out.println();}}}
}
03版为例:
excel内容
输出
FFFF:FFFF:0
9696:9696:9696
3333:3333:3333
3333:3333:3333
这篇关于Java 查看Excel单元格背景填充色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!