要求:使用java语言将 Excel 文件路径作为参数传递到 main 方法中,然后将其中的变量以键值对的形式保存(该变量需要在其他类中使用),并在另一个函数中(主函数)的控制台输出该键值对。
ExcelPair.java
这段代码定义了一个简单的Java类,用于表示Excel文件中的一对数据(商品名称和价格)。
package ExcelRead;
public class ExcelPair {
private final String commodityName;
private final float price;
public ExcelPair(String commodityName, float price) {
this.commodityName = commodityName;
this.price = price;
}
public String getCommodityName() {
return commodityName;
}
public float getPrice() {
return price;
}
}
ExcelRead.java
这段代码定义了一个名为"ExcelRead"的Java类,其中包含一个名为"readExcel"的静态方法。这个方法接收一个字符串参数"filePath",表示要读取的Excel文件的路径,然后返回一个ExcelPair对象列表。
package ExcelRead;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
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.ArrayList;
import java.util.List;
public class ExcelRead {
public static List<ExcelPair> readExcel(String filePath) throws Exception {
//创建一个空列表,用于存储从Excel文件中读取数据
List<ExcelPair> dataList = new ArrayList<>();
boolean skipFirstRow = true;//跳过excel的第一行,即标题行
File excelFile = new File(filePath);
if(!excelFile.exists()){
throw new Exception("Excel文件不存在!");
}
try (FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis)) {
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
if (skipFirstRow) {
skipFirstRow = false;
continue;
}
Cell cell1 = row.getCell(0);
String key = cell1.toString();
Cell cell2 = row.getCell(1);
float value = (float)cell2.getNumericCellValue();
ExcelPair pair = new ExcelPair(key, value);
dataList.add(pair);
}
} catch (Exception e) {
throw new Exception("无法读取 Excel 文件", e);
}
return dataList;
}
}
testRead.java
这段代码的作用是用于测试ExcelRead类中的"readExcel"方法是否能够正确地读取Excel文件,并将读取的数据打印在控制台上。
package ExcelRead;
import java.util.List;
public class testRead {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("未提供Excel文件路径作为命令行参数!");
return;
}
String filePath = args[0];
try {
List<ExcelPair> dataList = ExcelRead.readExcel(filePath);
for (ExcelPair pair : dataList) {
System.out.println(pair.getCommodityName() + ": " + pair.getPrice());
}
} catch (Exception e) {
System.err.println("无法读取 Excel 文件: " + e.getMessage());
e.printStackTrace();
}
}
}
pom.xml配置
<?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>org.example</groupId>
<artifactId>autoPrice</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
</project>