在硒中读取Excel文件时出错

时间:2018-09-24 21:09:32

标签: java excel

package ReadExcelData;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadandWriteExcel {

 public static void main(String []args){

  try {
      File src = new File("C:\\poi\\ExcelData\\TestExcelData.xlsx");
      FileInputStream fis=new FileInputStream(src);

      XSSFWorkbook wb=new XSSFWorkbook(fis);
      XSSFSheet sh1= wb.getSheetAt(0);
      System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());
      System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());
      System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());
      System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());
      System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());
      System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());
      wb.close();
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
 }

}

嗨,

我下载了poi-4.0.0并将外部jar文件添加到我的项目中。项目设置完成后,我创建了excel文件并编写了Java程序以从excel文件中读取

While run time I am getting error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
    at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
    at ReadExcelData.ReadandWriteExcel.main(ReadandWriteExcel.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 4 more`enter code here`

1 个答案:

答案 0 :(得分:1)

您需要将commons-compress-1.18.jar添加到项目构建路径中。 https://commons.apache.org/proper/commons-compress/download_compress.cgi