如何使用POI处理旧的excel .xls文件?

时间:2022-09-07 22:51:36

I switched from jxl to poi since POI has more features. However, I wasn't able to process the xls files that were generated in the old format. Now I am getting this error:

由于poi有更多的特性,我从jxl切换到poi。但是,我无法处理以旧格式生成的xls文件。现在我得到了这个错误:

org.apache.poi.hssf.OldExcelFormatException: The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)

org.apache.poi.hssf。OldExcelFormatException:提供的电子表格似乎是Excel 5.0/7.0 (BIFF5)格式。POI只支持BIFF8格式(来自97/2000/XP/2003的Excel版本)

Now I am thinking to use both JXL as wells as POI depending on the xls version so for old format xls files I will use jxl while for newer versions I will use POI. Is this a good solution? Are there any alternatives?

现在,我正在考虑根据xls版本将JXL用作POI,因此对于旧格式的xls文件,我将使用JXL,而对于新版本,我将使用POI。这是一个好的解决方案吗?有什么选择吗?

2 个解决方案

#1


11  

For old Excel format files, you have the following alternatives:

对于旧的Excel格式文件,您有以下选择:

  1. HSSF, the POI implementation of the Excel '97(-2007) file format.
    • If you just want to extract the textual content, then you can use OldExcelExtractor which will pull only the text and numbers from the file.
    • 如果您只想提取文本内容,那么可以使用OldExcelExtractor,它只从文件中提取文本和数字。
    • If you need the values from a specific cells, then you'll need to take an approach a bit like OldExcelExtractor, process the file at the record level, and check for the co-ordinates on OldStringRecord, NumberRecord, OldFormulaRecord and friends.
    • 如果需要来自特定单元格的值,则需要采用类似OldExcelExtractor的方法,在记录级别处理文件,并检查OldStringRecord、NumberRecord、oldformula arecord和friends上的坐标。
  2. HSSF是Excel '97(-2007)文件格式的POI实现。如果您只想提取文本内容,那么可以使用OldExcelExtractor,它只从文件中提取文本和数字。如果需要来自特定单元格的值,则需要采用类似OldExcelExtractor的方法,在记录级别处理文件,并检查OldStringRecord、NumberRecord、oldformula arecord和friends上的坐标。
  3. Like you already mentioned, JXL can handle some cases too.
  4. 正如您已经提到的,JXL也可以处理一些情况。
  5. Use a JDBC/ODBC driver. It is not as flexible as HSSF but for some old formats it is the only way to extract the information.
  6. 使用JDBC / ODBC驱动程序。它没有HSSF那么灵活,但是对于一些旧格式来说,它是提取信息的唯一方法。

#2


-4  

as per my knowledge you can use this code to read excel files of the .xls format

据我所知,您可以使用此代码读取.xls格式的excel文件

FileInputStream in=new FileInputStream(new File("filename.xls"));
Wookbook wb=new HSSFWorkbook(in);

to read the new excel versions(2007 and up):

阅读新版excel(2007及以上版本):

 FileInputStream in=new FileInputStream(new File("filename.xls"));
    Wookbook wb=new XSSFWorkbook(in);

external jar files that you will need:

您需要的外部jar文件:

 1. poi-3.9 
 2. dom4j-1.6.1
 3. XMLbeams-2.5.0

if you're work only requires you to work with .xls then only poi-3.0 will suffice. You need the other jars to work witht the new versions of excel.

如果您的工作只要求您使用.xls,那么只有poi-3.0就足够了。您需要其他jar来处理excel的新版本。

#1


11  

For old Excel format files, you have the following alternatives:

对于旧的Excel格式文件,您有以下选择:

  1. HSSF, the POI implementation of the Excel '97(-2007) file format.
    • If you just want to extract the textual content, then you can use OldExcelExtractor which will pull only the text and numbers from the file.
    • 如果您只想提取文本内容,那么可以使用OldExcelExtractor,它只从文件中提取文本和数字。
    • If you need the values from a specific cells, then you'll need to take an approach a bit like OldExcelExtractor, process the file at the record level, and check for the co-ordinates on OldStringRecord, NumberRecord, OldFormulaRecord and friends.
    • 如果需要来自特定单元格的值,则需要采用类似OldExcelExtractor的方法,在记录级别处理文件,并检查OldStringRecord、NumberRecord、oldformula arecord和friends上的坐标。
  2. HSSF是Excel '97(-2007)文件格式的POI实现。如果您只想提取文本内容,那么可以使用OldExcelExtractor,它只从文件中提取文本和数字。如果需要来自特定单元格的值,则需要采用类似OldExcelExtractor的方法,在记录级别处理文件,并检查OldStringRecord、NumberRecord、oldformula arecord和friends上的坐标。
  3. Like you already mentioned, JXL can handle some cases too.
  4. 正如您已经提到的,JXL也可以处理一些情况。
  5. Use a JDBC/ODBC driver. It is not as flexible as HSSF but for some old formats it is the only way to extract the information.
  6. 使用JDBC / ODBC驱动程序。它没有HSSF那么灵活,但是对于一些旧格式来说,它是提取信息的唯一方法。

#2


-4  

as per my knowledge you can use this code to read excel files of the .xls format

据我所知,您可以使用此代码读取.xls格式的excel文件

FileInputStream in=new FileInputStream(new File("filename.xls"));
Wookbook wb=new HSSFWorkbook(in);

to read the new excel versions(2007 and up):

阅读新版excel(2007及以上版本):

 FileInputStream in=new FileInputStream(new File("filename.xls"));
    Wookbook wb=new XSSFWorkbook(in);

external jar files that you will need:

您需要的外部jar文件:

 1. poi-3.9 
 2. dom4j-1.6.1
 3. XMLbeams-2.5.0

if you're work only requires you to work with .xls then only poi-3.0 will suffice. You need the other jars to work witht the new versions of excel.

如果您的工作只要求您使用.xls,那么只有poi-3.0就足够了。您需要其他jar来处理excel的新版本。