Apache POI - 处理数据的部分无效 [英] Apache POI - Invalid part to process data

查看:30
本文介绍了Apache POI - 处理数据的部分无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java Apache POI (hssf) 访问 Excel 电子表格.我收到以下错误:

I access an Excel spreadsheet using Java Apache POI (hssf). I got the following error :

java.lang.RuntimeException: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143)
at org.testng.internal.Parameters.handleParameters(Parameters.java:426)

我错过了什么?

推荐答案

打开 一个 xlsx (Office Open XML) 文件,你应该使用 XSSFWorkbook 而不是 HSSFWorkbook,用于 xls (Excel 97-2003) 文件.

To open an xlsx (Office Open XML) file, you should use XSSFWorkbook instead of HSSFWorkbook, which is used for xls (Excel 97-2003) files.

如果您使用 POI <3.5,你至少需要升级到3.5版本才能读取xlsx文件.这是用于进行转换的 指南,但本质上,您需要加载文件使用 WorkbookFactory 负责为您创建 XSSFWorkbookHSSFWorkbook:

If you are using POI < 3.5, you need to upgrade to at least version 3.5, in order to be able to read xlsx files. Here's a guide for doing the conversion, but essentially, you'd need to load the file using WorkbookFactory which takes care of the creation of either an XSSFWorkbook or HSSFWorkbook for you:

Workbook workbook = WorkbookFactory.create(new File("file.xlsx"))

这篇关于Apache POI - 处理数据的部分无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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