尝试使用poi apache库读取excel文件

时间:2015-10-06 10:31:44

标签: java excel apache

我正在尝试使用poi apache库读取excel文件。我尝试了不同类型的代码,但我的所有代码仍然遇到同样的错误。我不知道为什么会出现这个错误。

您可以从以下链接下载POI apache库: https://poi.apache.org/download.html

这是我读取excel文件的代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Pacer
 */
public class ReadExcelDemo
{
    public static void main(String[] args)
    {
        try
        {
            System.out.println("Working Directory = " + System.getProperty("user.dir"));
            FileInputStream file = new FileInputStream(new File("book.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext())
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType())
                    {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是我得到的错误:

Working Directory = E:\NetBeansProjects\Project24\CoverageCodetool

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at coveragecodetool.ReadExcelDemo.main(ReadExcelDemo.java:30)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
Java Result: 1

请帮忙!

1 个答案:

答案 0 :(得分:0)

你的类路径中缺少xmlbeans apache库。

将xmlbeans添加到类路径中,一切都会正常工作。

图书馆本身可以下载here

解析NoClassDefFoundException的一般算法如下:

  1. 搜索使用Exception中提到的类的库。我更喜欢this service
  2. 将库添加到类路径
  3. 尝试运行代码并查看问题是否仍然存在。
  4. 从第一步重复