gpt4 book ai didi

java - org.apache.poi 中的异常

转载 作者:行者123 更新时间:2023-12-02 01:10:26 26 4
gpt4 key购买 nike

我试图编写一个可以读取和写入 .xlsx 文件的程序,下面提供的代码旨在能够编写其第一个 Excel 程序。

package excel_reader;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelWriter{

public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
// first sheet create
HSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
// first row create - 1
HSSFRow row = sheet.createRow(0);
// first cell create - 1
HSSFCell cell = row.createCell(0); // A-1
// give data into A-1 cell
cell.setCellValue("Tester");

// Output as an excel file
workbook.write(new FileOutputStream("D:\\book1.xlsx"));
workbook.close();
}
}

不知何故,它无法在我提供的Excel表格上写入,请帮助我!

错误代码:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:102)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:124)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1373)
at excel_reader.ExcelWriter.main(ExcelWriter.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 8 more

最佳答案

一般来说,java.lang.NoClassDefFoundError:会在您缺少所需的 jar 时显示检查您是否已将 commons-math3 jar 添加到构建路径,如果没有,请添加下载或使用 maven 依赖项。

如果您的项目是 Maven 项目,请在 pom.xml 中添加此依赖项

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.0</version>
</dependency>

关于java - org.apache.poi 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59484870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com