Apache POI XSSF 读取 Excel 文件
我只是有一个关于如何使用 Apache 的 XSSF 格式读取 xlsx 文件的快速问题。
现在我的代码如下所示:
InputStream fs = new FileInputStream(filename); // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs); // (2)
XSSFSheet sheet = wb.getSheetAt(0); // (3)
...导入了所有相关内容。我的问题是,当我点击运行时,它卡在第 (2) 行,几乎陷入无限循环。 filename
只是一个字符串。
如果有人能给我一些关于如何解决这个问题的示例代码,我将非常感激。我现在想要的只是从 xlsx 文件中读取单个单元格;我对 xls 文件使用 HSSF,没有任何问题。
感谢您的帮助, 安德鲁
I just have a quick question about how to read in an xlsx file using the XSSF format from Apache.
Right now my code looks like this:
InputStream fs = new FileInputStream(filename); // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs); // (2)
XSSFSheet sheet = wb.getSheetAt(0); // (3)
...with all the relevant things imported. My problem is that when I hit run, it gets stuck at line (2), in almost an infinite loop. filename
is just a string.
If anybody could give me some sample code on how to fix this I would really appreciate it. All i want right now is to read in a single cell from an xlsx file; I was using HSSF for xls files and had no problems.
Thanks for your help,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我相信这会回答您的问题: http://poi.apache.org/ Spreadsheet/quick-guide.html#ReadWriteWorkbook
简而言之,您的代码应如下所示:
I bealive that this will answer your questions: http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook
In short, your code should look like this:
为什么要将文件分解为输入流? XSSFWorkbook 有一个构造函数,它仅将路径作为字符串。只需对字符串的路径进行硬编码即可。创建工作簿后,您可以从中创建 XSSFSheets。然后是 XSSFCells,它最终将允许您读取单个单元格的内容(单元格本质上基于 x,y 位置)
Why are you breaking the file into an InputStream? XSSFWorkbook has a constructor that simply takes the path as a String. Just hard code the path of the string in. Once you create the workbook you can create XSSFSheets from that. Then XSSFCells, which will then finally allow you to read the contents of a single cell (cells are based on x,y locations, essentially)
您可以尝试以下操作。
You can try the following.
这工作正常:尝试一下
this works fine: try it
我有同样的错误,我刚刚用相同的版本更新了 pom 依赖项。它起作用了。
I have the same error, I have just updated the pom dependencies with the same version. It worked.
这里的代码仅识别第一张工作表 - 在我的 Excel 中存在多个工作表,因此面临将
sheet2
更改为sheet1(getSheetAt(0))
的问题...Here code recognizing only first sheet - In my Excel multiple sheets are there so facing problem to change
sheet2
tosheet1(getSheetAt(0))
...