gpt4 book ai didi

java - 使用java和apache POI读取Excel中的数据

转载 作者:行者123 更新时间:2023-11-30 05:23:25 27 4
gpt4 key购买 nike

我正在尝试使用 Apache POI 文件读取数据并将其写入 Excel。我已经导入了必要的 jar ,即:poi.4.1.1.jarxmlbeanspoi-ooxmlpoi-ooxml-架构

我当前编写的以下代码在“Workbook wb = WorkbookFactory.create(fis);”行失败:

package readingData;

import java.io.FileInputStream;
import java.io.FileOutputStream;

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.ss.usermodel.WorkbookFactory;

public class ReadDatafromExcel {

private static Sheet sh;
private static Row row;
private static Cell cell;
private static FileInputStream fis;
private static FileOutputStream fos;

public static void main(String[] args) throws Exception {
fis = new FileInputStream("C:/Users/10610985/workspace/ReadData/SampleData.xlsx");
Workbook wb = WorkbookFactory.create(fis);
// sh = wb.getSheetAt(0);
//// int firstRow = sh.getFirstRowNum();
//// System.out.println(firstRow);
//
// int lastRow = sh.getLastRowNum();
// System.out.println(lastRow);
}
}

下面是我收到的错误消息:

Exception in thread "main" java.io.IOException: org/apache/commons/compress/archivers/zip/ZipFile
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:353)
at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:316)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:234)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:200)
at readingData.ReadDatafromExcel.main(ReadDatafromExcel.java:22)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:307)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:341)
... 4 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 11 more

我尝试在网上搜索此问题,但找不到解决方案。关于我哪里出错的任何想法,或者我是否可以做任何事情来理解 Workbookfactory 中的各种方法。

以下是添加 apache common compress jars 后更新的错误消息。

Exception in thread "main" java.io.IOException: org/apache/commons/collections4/ListValuedMap
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:353)
at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:316)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:234)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:200)
at readingData.ReadDatafromExcel.main(ReadDatafromExcel.java:22)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:88)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:341)
... 4 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 11 more

问题已解决

从这里添加了 apache 通用压缩 jar。 https://commons.apache.org/proper/commons-compress/download_compress.cgi

从这里添加了通用集合 4-4.1 jar。 https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

最佳答案

Add commons-collections4-4.1.jar file in your build path and try it again. It will work.

You can find it here https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

if you are not using maven you can directly download the jar and add it to your project

关于java - 使用java和apache POI读取Excel中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59138360/

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