使用Java和Apache POI在excel中读写数据

时间:2019-12-02 11:49:43

标签: java excel apache-poi

我正在尝试使用Apache POI文件读写数据到excel。我已经导入了必要的罐子: poi.4.1.1.jar xmlbeans poi-ooxml poi-ooxml-schemas

我目前正在编写以下代码,该代码在“ 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通用压缩罐后的更新错误消息。

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常见压缩罐。 https://commons.apache.org/proper/commons-compress/download_compress.cgi

从此处添加了通用收藏4-4.1罐。 https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

  FileOutputStream fileOut = new FileOutputStream("your file path");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("sheet name");
            try {

    //creating the headers          
                Row row = sheet.createRow(0);
                row.createCell(0).setCellValue("name");
                row.createCell(1).setCellValue("Name1");                    
                row.createCell(2).setCellValue("name2");

    //get the list which u want
                List<String> list = getNamesList();                         
            int rowNum = 1;
            for (Name name : list ) {
                Row row1 = sheet.createRow(rowNum++);
        row1.createCell(0).setCellValue((name.get..));

}
                workbook.write(fileOut);
}
shareeditdeleteflag

    enter code here