POI Workbook interfaces and Hssfworkbook objects and Xssfworkbook objects manipulate the corresponding Excel version

Source: Internet
Author: User
Just started using new Hssfworkbook (Excelfile) to read workbook, no problem with the previous version of Excel2003 (including 2003), FileInputStream However, the following exception occurred while reading Excel2007:
Org.apache.poi.poifs.filesystem.OfficeXmlFileException:The supplied data appears to being in the Office 2007+ XML. You are calling the "part of POI" deals with OLE2 Office Documents. You are need to call a different part of the POI to process this data (eg XSSF instead of HSSF)
This error means that the data in the file is saved with Office2007+xml and now calls OLE2 Office document processing, and you should use a different part of POI to process the data, such as using XSSF instead of HSSF.
Then the prompts to use XSSF instead of HSSF, with the new Xssfworkbook (excelfile) to read workbook, no problem with the Excel2007, However, the following new exception occurred while reading the previous version of Excel2003 (including 2003):
Org.apache.poi.openxml4j.exceptions.InvalidOperationException:Can ' t open the specified file: ' *.xls '
The error is that the operation is invalid and the specified XLS file cannot be opened.
Download poi of the source code after a single step debugging, found that the beginning of the time is still right, but to ZipFile after the class can not find the file, to the online check, the original is XSSF can not read Excel2003 before (including 2003) version, So, You need to decide before reading whether the file is a version of 2003 or 2007, and then read the call HSSF or XSSF.
In short: Because Hssfworkbook can only operate excel2003 version, Xssfworkbook can only operate excel2007 the above version, so use the workbook interface to create the corresponding object to manipulate Excel to handle compatibility

@Test
public void Test6 () throws exception{
Hssfworkbook workbook = new Hssfworkbook (New FileInputStream ("C://123.xls"));
Hssfsheet sheet = workbook.getsheetat (0);
Hssfrow row =sheet.getrow (0);
Hssfcell cell= Row.getcell (0);
System.out.println (Cell.tostring ());
}

@Test
public void Test7 () throws exception{
Xssfworkbook workbook = new Xssfworkbook (New FileInputStream ("c://456.xlsx"));
Xssfsheet sheet = workbook.getsheetat (0);
Xssfrow row =sheet.getrow (0);
Xssfcell cell= Row.getcell (0);
System.out.println (Cell.tostring ());
}

@Test//Use workbook interface and Judge Excel version to create the corresponding version Hssfworkbook/xssfworkbook object
public void Test8 () throws exception{
String file = "c://456.xlsx";
Boolean isExcel2003 = File.tolowercase (). EndsWith ("xls")? True:false;
Workbook workbook = null;
if (isExcel2003) {
workbook = new Hssfworkbook (new FileInputStream (file));
}else{
workbook = new Xssfworkbook (new FileInputStream (file));
}
Sheet Sheet = workbook.getsheetat (0);
Row row =sheet.getrow (0);
Cell cell= Row.getcell (0);
System.out.println (Cell.tostring ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.