0
我想讀取使用poi apache庫的excel文件。我嘗試了不同類型的代碼,但仍然得到了與我所有代碼相同的錯誤。我不知道爲什麼會出現這個錯誤。試圖讀取使用poi apache庫的excel文件
你可以從這個鏈接下載POI阿帕奇庫: 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
請幫忙!
編輯答案 – WeMakeSoftware
好的。我添加了import org.apache.xmlbeans.XmlException;正如所建議的,我得到了一些錯誤: 線程「主」的異常java.lang.NoClassDefFoundError:org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument – Mitesh