【问题标题】:Getting error while accessing excel data using apache POI and java使用 apache POI 和 java 访问 excel 数据时出错
【发布时间】:2020-03-31 04:32:49
【问题描述】:

我有一个函数,它采用 Excel 路径和工作表名称并返回行数的 int 值,但是当我尝试运行它时,它给出了以下错误。我是java新手,所以可能是因为我太笨了哈哈。

我的系统:

  1. Netbeans 8.2
  2. jdk 1.8
  3. Apache poi-src-4.1.2-20200217
  4. Kubuntu 最新

我的意见:

  1. ExcelPath:/media/bigdata/Downloads/text2.xls
  2. 工作表名称:工作表 1

代码:

package com.test.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewClass {

 private static FileInputStream fis;
 private static FileOutputStream fileOut;
 private static XSSFWorkbook wb;
 private static XSSFSheet sh;
 private static XSSFCell cell;
 private static XSSFRow row;
 private static XSSFCellStyle cellstyle;
 private static XSSFColor mycolor;

 public static int setExcelFile(String ExcelPath,String SheetName) throws Exception
 { 
    int noOfRows = -1;
    try{  
       File f = new File(ExcelPath);
       if(!f.exists()){
          System.out.println("File doesn't exist.");
        }  
       else{
            fis=new FileInputStream(ExcelPath);
            System.out.println(fis);
            wb=new XSSFWorkbook(fis);
            sh = wb.getSheet(SheetName);
            //sh = wb.getSheetAt(0); //0 - index of 1st sheet
            if (sh == null)
            {
                sh = wb.createSheet(SheetName);
            }
       }
       noOfRows = sh.getLastRowNum();  
     }catch (IOException e){System.out.println(e.getMessage());}
return (noOfRows);
 }

}

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at com.test.system.NewClass.setExcelFile(NewClass.java:45)
    at com.test.system.NewMain.main(NewMain.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    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)
    ... 2 more

我尝试了一切,但仍然没有成功。

【问题讨论】:

标签: java excel apache-poi


【解决方案1】:

在 else 语句中你应该写

fis=new FileInputStream(f);

而不是

fis=new FileInputStream(ExcelPath);

你不能使用:System.out.println(fis); //它不会工作

【讨论】:

  • 仍然得到相同的结果。异常:线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/xmlbeans/XmlException
最近更新 更多