我一直在尝试使用Java程序生成Invoice语句,并且遇到了使用Apache POI处理存储正在访问的信息的.xlsm的问题。
Invoice.java
import java.lang.*;
import java.io.*;
import java.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.*;
public class Invoice {
String _file = "vendors.xlsm";
int vs = 0;
int ps = 0;
public Invoice(String file, int vendSheet, int paySheet) {
_file = file;
vs = vendSheet;
ps = paySheet;
}
public Invoice() {
}
public void getVendors() {
//String[][] vendors = new string[][]; commented out for testing purposes
try {
FileInputStream fis = new FileInputStream(new File(_file));
Workbook wb = new XSSFWorkbook(fis);
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> iRow = sheet.iterator();
// Reading something finally (rows from the vendor sheet)
while(iRow.hasNext()) {
Row currRow = iRow.next();
Iterator<Cell> iCell = currRow.iterator();
// comparing cell types here
while(iCell.hasNext()) {
Cell currCell = iCell.next();
if(currCell.getCellType() == CellType.STRING) {
System.out.print(currCell.getStringCellValue() + "--");
}
else if(currCell.getCellType() == CellType.NUMERIC) {
System.out.print(currCell.getNumericCellValue() + "--");
}
}
System.out.println();
}
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我遇到的问题发生在Workbook wb = new XSSFWorkbook(fis);
我得到以下错误输出:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at Invoice.getVendors(Invoice.java:37)
at InvoiceBuilder.main(InvoiceBuilder.java:32)
... 11 more
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
Exception running application InvoiceBuilder
它总是指向那一行。我不太确定从这里去哪里,我还找不到解决方案。
如果文件路径看起来不正确,那是因为我删节了文件路径,以免我的名字出名。