Read data from read only xlsm file using Java Apache POI

霸气de小男生 提交于 2019-12-11 01:56:59

问题


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:

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!


回答1:


I copied your code and gave the absolute path of 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.




回答2:


Version 4.0.0 has a workbook factory class with a boolean "readonly" parameter. So, to open in read-only mode...

XSSFWorkbook nextWorkbook = XSSFWorkbookFactory.createWorkbook(myFile, true);


来源:https://stackoverflow.com/questions/24317688/read-data-from-read-only-xlsm-file-using-java-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!