在尝试使用ApachePOI打开excel时,我收到
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\Users\mdwaipay\AppData\Local\Temp\poifiles\poi-ooxml-1570030023.tmp'
我查过了。没有创建这样的文件夹。我使用的是Apache POI版本3.6。
有什么帮助吗?类似的代码在不同的工作空间中运行良好。在这里失去了思想。
代码:
public Xls_Reader(String path) {
this.path=path;
try {
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
}
catch (Exception e)
{ e.printStackTrace();
}
}发布于 2012-09-14 16:45:23
为什么你要拿一个非常好的文件,把它包装在一个InputStream中,然后要求POI必须为你缓冲整个文件,以便它可以进行随机访问?如果您只需将文件直接传递到POI,生活将会更好,因此它可以根据需要跳过!
如果您想同时使用XSSF (.xlsx)和HSSF (.xls),请将代码更改为
public Xls_Reader(String path) {
this.path = path;
try {
File f = new File(path);
workbook = WorkbookFactory.create(f);
sheet = workbook.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
}
}如果您只想要XSSF支持,并且/或者您需要完全控制资源何时关闭,那么可以这样做
OPCPackage pkg = OPCPackage.open(path);
Workbook wb = new XSSFWorkbook(pkg);
// use the workbook
// When you no longer needed it, immediately close and release the file resources
pkg.close();https://stackoverflow.com/questions/12350775
复制相似问题