簡體   English   中英

使用apache POI和java訪問excel數據時出錯

[英]Getting error while accessing excel data using apache POI and java

我有一個函數,它接受 Excel 路徑和工作表名稱並返回行數的 int 值,但是當我嘗試運行它時,它給出了以下錯誤。 我是 Java 新手,所以可能是因為我太愚蠢了哈哈。

我的系統:

  1. 網豆 8.2
  2. JDK 1.8
  3. Apache poi-src-4.1.2-20200217
  4. 庫本圖最新

我的輸入:

  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

我嘗試了一切,但仍然沒有成功。

在 else 語句中你應該寫

fis=new FileInputStream(f);

代替

fis=new FileInputStream(ExcelPath);

並且您不能使用: System.out.println(fis); //它不會工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM