问题描述
我试图编写一个可以读写 .xlsx
文件的程序,下面提供的代码旨在能够编写它的第一个 excel 程序.
I was trying to write a program that it can read and write a .xlsx
file, the code provided below is designed to be able to write its first excel program.
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表上写不出来,请帮帮我!
Somehow, it cannot write the on the excel sheet that I provided, please do help me!
错误代码:
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: 会在您缺少所需的 jars 时显示检查您是否已将 commons-math3 jar 添加到您的构建路径中,如果没有,请添加下载或使用 maven 依赖项.
In general the java.lang.NoClassDefFoundError: shows when you are missing the required jarsCheck if you have added commons-math3 jar to your build path, if not please add either downloading or using maven dependency.
如果你的项目是一个 Maven 项目,那么在你的 pom.xml 中添加这个依赖
If your's is a maven project then add this dependency in your pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.0</version>
</dependency>
这篇关于org.apache.poi 中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!