繁体   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