<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>rg</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- <!–mysql依赖–>-->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>8.0.23</version>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
</project>
package com.rg;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelUtil {
public String excelPath() {
String excelPath = "D:\\pro\\rg\\src\\main\\resources\\1主题模型表_PL00_202109291418.xlsx";
return excelPath;
}
public File excel(String excelPath) {
File excel = new File(excelPath);
if (excel.isFile() && excel.exists()) {
return excel;
}
throw new RuntimeException("文件不存在");
}
public String excelType(String excelPath) {
String[] strs = excelPath.split("\\.");
if (strs == null || strs.length < 1) {
throw new RuntimeException("文件类型异常");
}
int len = strs.length;
String excelType = strs[len - 1];
if ("xls".equals(excelType)) {
return "xls";
} else if ("xlsx".equals(excelType)) {
return "xlsx";
} else {
throw new RuntimeException("文件类型异常");
}
}
public Workbook getWorkbook(String excelType) throws IOException, InvalidFormatException {
Workbook wb = null;
if ("xls".equals(excelType)) {
FileInputStream fis = new FileInputStream(excel(excelType)); //文件流对象
wb = new HSSFWorkbook(fis);
} else if ("xlsx".equals(excelType)) {
wb = new XSSFWorkbook(excel(excelPath()));
}
return wb;
}
public void readSheet(Workbook workbook, int sheetPage) {
Sheet sheet = workbook.getSheetAt(sheetPage);
int firstRowIndex = sheet.getFirstRowNum() + 2; //第一行是列名,所以不读
int lastRowIndex = sheet.getLastRowNum();
for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) { //遍历列
Cell cell = row.getCell(cIndex);
if (cell != null) {
System.out.printf("%s ",cell.toString());
}
}
System.out.println();
}
}
}
}
package com.rg;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InvalidFormatException {
ExcelUtil excelUtil=new ExcelUtil();
excelUtil.readSheet(excelUtil.getWorkbook(excelUtil.excelType(excelUtil.excelPath())),0);
}
}