zoukankan      html  css  js  c++  java
  • 关于POI解析Excel文件(03和07版本不同)的问题

    问题描述:在使用poi包进行excel解析时,发现对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:
    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)。

    原因分析:XSSF不能读取Excel2003以前(包括2003)的版本,如果遇到2007版本的Excel文件就需要在读取前判断文件是2003前的版本还是2007的版本,然后对应调用HSSF或XSSF来读取。

    解决方式:目前只能通过后缀名来判断文件版本,代码如下:(如果有高手知道别的解决方法,请告知,谢谢!)

    1 public static boolean isExcel2003(String filePath)  
    2 {  
    3   
    4     return filePath.matches("^.+\.(?i)(xls)$");  
    5 
    6 }  

    获得版本号之后,根据版本号的不同创建不同的对象,代码如下:

     1 /** 根据版本选择创建Workbook的方式 */  
     2   
     3 Workbook wb = null;  
     4   
     5 if (isExcel2003)  
     6 {  
     7    wb = new HSSFWorkbook(inputStream);  
     8 }  
     9 else  
    10 {  
    11    wb = new XSSFWorkbook(inputStream);  
    12 }  
  • 相关阅读:
    Atcoder Grand Contest 003 题解
    Atcoder Grand Contest 002 题解
    Atcoder Grand Contest 001 题解
    网络流24题
    AGC005D ~K Perm Counting
    loj6089 小Y的背包计数问题
    CF932E Team Work
    组合数学相关
    SPOJ REPEATS
    [SDOI2008]Sandy的卡片
  • 原文地址:https://www.cnblogs.com/handsomeye/p/5172058.html
Copyright © 2011-2022 走看看