尝试使用工作簿wb = WorkbookFactory.create(new File(fileName)); //阅读以下链接中的xls文件
尝试了其他一些库jxcel,没有运气,如果做错了事,有人可以帮忙
XLS文件:https://www.huduser.gov/portal/datasets/excel/wy_foreclosure.zip
代码:
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
.......
......
try (InputStream is = new FileInputStream(new File(fileName));
Workbook wb = WorkbookFactory.create(new File(fileName));) {
}
异常跟踪:
Exception in thread "main" org.apache.poi.util.RecordFormatException: Not enough data (0) to read requested (2) bytes
at org.apache.poi.hssf.record.RecordInputStream.checkRecordPosition(RecordInputStream.java:243)
at org.apache.poi.hssf.record.RecordInputStream.readShort(RecordInputStream.java:262)
at org.apache.poi.hssf.record.PrintSetupRecord.<init>(PrintSetupRecord.java:74)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.poi.hssf.record.RecordFactory$ReflectionConstructorRecordCreator.create(RecordFactory.java:84)
at org.apache.poi.hssf.record.RecordFactory.createSingleRecord(RecordFactory.java:345)
at org.apache.poi.hssf.record.RecordFactoryInputStream.readNextRecord(RecordFactoryInputStream.java:288)
at org.apache.poi.hssf.record.RecordFactoryInputStream.nextRecord(RecordFactoryInputStream.java:254)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:494)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:344)
at org.apache.poi.hssf.usermodel.HSSFWorkbookFactory.createWorkbook(HSSFWorkbookFactory.java:49)
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 org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:314)
at org.apache.poi.ss.usermodel.WorkbookFactory.createHSSFWorkbook(WorkbookFactory.java:292)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:128)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:74)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:281)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:253)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:234)
at com.noteunlimited.util.ForeClosureDump.generateStatistics(ForeClosureDump.java:55)
at com.noteunlimited.util.ForeClosureDump.main(ForeClosureDump.java:64)
答案 0 :(得分:0)
要打开现有的XLS文件,您可以例如使用此代码:
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class OpenXLS {
public static void main(String[] args) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("yourFile.xls"));
// Do something with it.
workbook.close();
}
}
这不适用于您提供的文件。但是,当您在LO Calc中打开这些文件之一并将其保存而不进行任何更改时,一切都很好。因此,您的文件似乎不正确。