使用 Java Apache POI 从只读 xlsm 文件中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24317688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read data from read only xlsm file using Java Apache POI
提问by ajs
I'm trying to read data from a read only xlsm using java apache poi, but when I use XSSF workbook it doesn't seem to be able to access the file and HSSF workbooks only work for xls files. My code looks like this:
我正在尝试使用 java apache poi 从只读 xlsm 读取数据,但是当我使用 XSSF 工作簿时,它似乎无法访问该文件,而 HSSF 工作簿仅适用于 xls 文件。我的代码如下所示:
try {
FileInputStream file = new FileInputStream(new File("file.xlsm"));
System.out.println("found file");
XSSFWorkbook workbook = new XSSFWorkbook(file);
System.out.println("in workbook");
XSSFSheet sheet = workbook.getSheet("Shipments");
System.out.println("got sheet");
The code never reaches the "in workbook" print line and I'm not sure why. Please help!
代码永远不会到达“工作簿中”打印行,我不知道为什么。请帮忙!
采纳答案by Pjarenee
I copied your code and gave the absolute pathof the file, and I was able to get the expected result.
我复制了你的代码并给出了文件的绝对路径,我能够得到预期的结果。
FileInputStream file = new FileInputStream(new File("C:\Users\user\Desktop\filet.xlsm"));
System.out.println("found file");
XSSFWorkbook workbook = new XSSFWorkbook(file);
System.out.println("in workbook");
XSSFSheet sheet = workbook.getSheet("Shipments");
System.out.println("got sheet");
It seems like a problem with your referencing. Therefore check the way you refer to the files when you try to create the file resource.
您的引用似乎有问题。因此,当您尝试创建文件资源时,请检查您引用文件的方式。
回答by ssimm
Version 4.0.0 has a workbook factory class with a boolean "readonly" parameter. So, to open in read-only mode...
版本 4.0.0 有一个带有布尔值“只读”参数的工作簿工厂类。因此,要以只读模式打开...
XSSFWorkbook nextWorkbook = XSSFWorkbookFactory.createWorkbook(myFile, true);