确认需要读取的表格的内容:
导入依赖:
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.1</version></dependency>
package com.example;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;/*** @author wqd* @description aa* @date 2021/9/24 9:53*/
public class AA{static XSSFRow row;public static void main(String[] args) throws Exception {FileInputStream fis = new FileInputStream(new File("G:\\a.xlsx"));XSSFWorkbook workbook = new XSSFWorkbook(fis);//获取第一张表XSSFSheet spreadsheet = workbook.getSheetAt(0);//遍历Iterator<Row> rowIterator = spreadsheet.iterator();while (rowIterator.hasNext()) {//获取行的集合row = (XSSFRow) rowIterator.next();//获取列的集合并遍历Iterator<Cell> cellIterator = row.cellIterator();while (cellIterator.hasNext()) {Cell cell = cellIterator.next();switch (cell.getCellType()) {//数字类型case NUMERIC:System.out.print(cell.getNumericCellValue() + " \t\t ");break;//字符串类型case STRING:System.out.print(cell.getStringCellValue() + " \t\t ");break;}}System.out.println();}workbook.close();fis.close();}}
读取数据如下: