使用 Java Apache POI 从只读 xlsm 文件中读取数据 [英] Read data from read only xlsm file using Java Apache POI

查看:72
本文介绍了使用 Java Apache POI 从只读 xlsm 文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java apache poi 从只读 xlsm 中读取数据,但是当我使用 XSSF 工作簿时,它似乎无法访问该文件,并且 HSSF 工作簿仅适用于 xls 文件.我的代码如下所示:

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!

推荐答案

我复制了你的代码并给出了文件的绝对路径,我能够得到预期的结果.

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.

这篇关于使用 Java Apache POI 从只读 xlsm 文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆