烦请老师看一下,为什么会报错呢?~谢谢(感觉是 workbook没有获取到值~~但不知道为什么~)
报错信息如下:
代码如下:
package com.hogwarts.junit5readFile;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.endsWithIgnoringCase;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class JunitExcelTest {
@Test
@DisplayName("读取excel文件内容放入一个列表中")
void withExcelFile() throws FileNotFoundException {
try {
//1、 获取文件名后缀
String filepath = "src/test/resources/source.xlsx";
String excelType = filepath.substring(filepath.lastIndexOf(".")+1,filepath.length());
assertThat("当前文件不是excel文件",excelType,anyOf(endsWithIgnoringCase("xlsx"),endsWithIgnoringCase("xls")));
//2、查看文件是否存在
File file = new File(filepath);
assertTrue(file.exists(),"文件不存在");
//3、获取工作薄对象Work --如果是xls结尾用HSSF--如果是xlsx结尾用XSSF
FileInputStream stream = new FileInputStream(file);
Workbook workbook = null;
if(excelType.equalsIgnoreCase("xlsx")){
workbook = new XSSFWorkbook(stream);
}else if(excelType.equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(stream);
}
//对应sheet是从0开始的
Sheet sheet = workbook.getSheetAt(0);
//当前工作薄有多少个sheet
int numsheet = workbook.getNumberOfSheets();
System.out.println("当前工作薄有"+numsheet+"sheet");
//当前sheet有多少行
int numrows = sheet.getPhysicalNumberOfRows();
System.out.println("当前sheet有"+numrows+"行");
for (Row row:sheet) {
System.out.println(row);
}
} catch (IOException e) {
e.printStackTrace();
}
//4、读取第一个sheet
//5、获取行
//6、读取每一行的单元格内容
//7、最后关闭流
}
POM文件如下:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>org.example</groupId>
<artifactId>Junit5Basics</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--junit5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<!-- 读取文件公共依赖-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--maven运行插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
</plugins>
</build>