org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file

来源:互联网 发布:使用手册制作软件 编辑:程序博客网 时间:2024/05/01 18:56

http://fengsky491.iteye.com/blog/537458

环境和所需包:

       1,JDK1.5

       2,poi-3.5-FINAL-20090928.jar,

            poi-contrib-3.5-FINAL-20090928.jar,

            poi-ooxml-3.5-FINAL-20090928.jar,

            poi-scratchpad-3.5-FINAL-20090928.jar,

            log4j-1.2.13.jar,

            commons-logging-1.1.jar,

            junit-3.8.1.jar,

            dom4j-1.6.1.jar,

            geronimo-stax-api_1.0_spec-1.0.jar,

            ooxml-schemas-1.0.jar,

            xmlbeans-2.3.0.jar

 

            注意

                    1,可能有些包不需要,没有测试,因为有些包项目中已经存在了

                    2,我开始少了最后2个包,报:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException

 

 

Java代码:

Java代码 复制代码 收藏代码
  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.InputStream;   
  4.   
  5. import org.apache.poi.hssf.usermodel.HSSFDateUtil;   
  6. import org.apache.poi.xssf.usermodel.XSSFCell;   
  7. import org.apache.poi.xssf.usermodel.XSSFRow;   
  8. import org.apache.poi.xssf.usermodel.XSSFSheet;   
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;   
  10.   
  11. public class POIExcelDemo {   
  12.   
  13.     public static void read(String fileName) throws Exception {   
  14.            
  15.         XSSFWorkbook wb = new XSSFWorkbook(fileName);   
  16.         read(wb);   
  17.     }   
  18.        
  19.     public static void read(InputStream is) throws Exception {   
  20.         XSSFWorkbook wb = new XSSFWorkbook(is);   
  21.         read(wb);   
  22.     }   
  23.        
  24.     public static void read(XSSFWorkbook xwb) throws Exception {   
  25.         try {   
  26.                
  27.             for (int k = 0; k < xwb.getNumberOfSheets(); k++) {   
  28.   
  29.                 XSSFSheet sheet = xwb.getSheetAt(k);   
  30.                 int rows = sheet.getPhysicalNumberOfRows();   
  31.   
  32.                 for (int r = 0; r < rows; r++) {   
  33.                     // 定义 row   
  34.                     XSSFRow row = sheet.getRow(r);   
  35.                     if (row != null) {   
  36.                         int cells = row.getPhysicalNumberOfCells();   
  37.   
  38.                         for (short c = 0; c < cells; c++) {   
  39.                             XSSFCell cell = row.getCell(c);   
  40.                             if (cell != null) {   
  41.                                 String value = null;   
  42.   
  43.                                 switch (cell.getCellType()) {   
  44.   
  45.                                 case XSSFCell.CELL_TYPE_FORMULA:   
  46.                                     value = "FORMULA ";   
  47.                                     break;   
  48.   
  49.                                 case XSSFCell.CELL_TYPE_NUMERIC:   
  50.                                     if(HSSFDateUtil.isCellDateFormatted(cell)){   
  51.                                         value = "DATE value="  
  52.                                             + cell.getDateCellValue();   
  53.                                     }else{   
  54.                                         value = "NUMERIC value="  
  55.                                                 + cell.getNumericCellValue();   
  56.                                     }   
  57.                                        
  58.                                     break;   
  59.   
  60.                                 case XSSFCell.CELL_TYPE_STRING:   
  61.                                     value = "STRING value="  
  62.                                             + cell.getStringCellValue();   
  63.                                     break;   
  64.                                        
  65.                                 case XSSFCell.CELL_TYPE_BOOLEAN:   
  66.                                     value = "BOOLEAN value="  
  67.                                             + cell.getBooleanCellValue();   
  68.                                        
  69.                                        
  70.                                     cell.getDateCellValue();   
  71.                                        
  72.                                     break;   
  73.   
  74.                                 default:   
  75.                                 }   
  76.                                    
  77.                                 System.out.println(value);   
  78.   
  79.                             }   
  80.                         }   
  81.                     }   
  82.                 }   
  83.             }   
  84.         } catch (Exception e) {   
  85.   
  86.             e.printStackTrace();   
  87.         }   
  88.   
  89.     }   
  90.   
  91.     /**  
  92.      * @param args  
  93.      * @throws Exception   
  94.      */  
  95.     public static void main(String[] args) throws Exception {   
  96.         // TODO Auto-generated method stub  
  97.   
  98.         File f = new File("d:/Test.xlsx");   
  99.            
  100.         FileInputStream is = new FileInputStream(f);   
  101.            
  102.         System.out.println(f.getName());   
  103.            
  104.         read(is);   
  105.            
  106.            
  107.     }   
  108.   
  109. }  

 

写完之后完把文件改成读取test.xls(Excel2003),发现出现Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\DOCUME~1\CHENXI~1\LOCALS~1\Temp\poifiles\poi-ooxml-1966473540.tmp'
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)

 

所以为了兼容读取2003,我把代码给成如下:

Java代码 复制代码 收藏代码
  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.InputStream;   
  4.   
  5. import org.apache.poi.hssf.usermodel.HSSFDateUtil;   
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  7. import org.apache.poi.poifs.filesystem.POIFSFileSystem;   
  8. import org.apache.poi.ss.usermodel.Cell;   
  9. import org.apache.poi.ss.usermodel.Row;   
  10. import org.apache.poi.ss.usermodel.Sheet;   
  11. import org.apache.poi.ss.usermodel.Workbook;   
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;   
  13.   
  14. public class POIExcelDemo {   
  15.   
  16.     /**  
  17.      *   
  18.      * @param fileName 文件路径  
  19.      * @param flag 是2003还是2007 true:2003,false:2007 
  20.      * @throws Exception  
  21.      */  
  22.     public static void read(String fileName,boolean flag) throws Exception {   
  23.         Workbook wb = null;   
  24.         if(flag){//2003  
  25.             File f = new File(fileName);   
  26.                
  27.             FileInputStream is = new FileInputStream(f);   
  28.             POIFSFileSystem fs = new POIFSFileSystem(is);      
  29.             wb = new HSSFWorkbook(fs);   
  30.             is.close();   
  31.         }else{//2007  
  32.             wb = new XSSFWorkbook(fileName);   
  33.         }   
  34.            
  35.         read(wb);   
  36.     }   
  37.        
  38.     /**  
  39.      *   
  40.      * @param is 输入流  
  41.      * @param flag 是2003还是2007 true:2003,false:2007 
  42.      * @throws Exception  
  43.      */  
  44.     public static void read(InputStream is,boolean flag) throws Exception {   
  45.         Workbook wb = null;   
  46.            
  47.         if(flag){//2003  
  48.             wb = new HSSFWorkbook(is);   
  49.         }else{//2007  
  50.             wb = new XSSFWorkbook(is);   
  51.         }   
  52.            
  53.         read(wb);   
  54.     }   
  55.        
  56.     /**  
  57.      * 具体读取Excel  
  58.      * @param wb  
  59.      * @throws Exception  
  60.      */  
  61.     public static void read(Workbook wb) throws Exception {   
  62.         try {   
  63.                
  64.             for (int k = 0; k < wb.getNumberOfSheets(); k++) {   
  65.   
  66.                 //sheet   
  67.                 Sheet sheet = wb.getSheetAt(k);   
  68.                 int rows = sheet.getPhysicalNumberOfRows();   
  69.   
  70.                 for (int r = 0; r < rows; r++) {   
  71.                     // 定义 row   
  72.                     Row row = sheet.getRow(r);   
  73.                     if (row != null) {   
  74.                         int cells = row.getPhysicalNumberOfCells();   
  75.   
  76.                         for (short c = 0; c < cells; c++) {   
  77.                             Cell cell = row.getCell(c);   
  78.                             if (cell != null) {   
  79.                                 String value = null;   
  80.   
  81.                                 switch (cell.getCellType()) {   
  82.   
  83.                                 case Cell.CELL_TYPE_FORMULA:   
  84.                                     value = "FORMULA value=" + cell.getCellFormula();   
  85.                                     break;   
  86.   
  87.                                 case Cell.CELL_TYPE_NUMERIC:   
  88.                                     if(HSSFDateUtil.isCellDateFormatted(cell)){   
  89.                                         value = "DATE value="  
  90.                                             + cell.getDateCellValue();   
  91.                                     }else{   
  92.                                         value = "NUMERIC value="  
  93.                                                 + cell.getNumericCellValue();   
  94.                                     }   
  95.                                        
  96.                                     break;   
  97.   
  98.                                 case Cell.CELL_TYPE_STRING:   
  99.                                     value = "STRING value="  
  100.                                             + cell.getStringCellValue();   
  101.                                     break;   
  102.                                        
  103.                                 case Cell.CELL_TYPE_BOOLEAN:   
  104.                                     value = "BOOLEAN value="  
  105.                                             + cell.getBooleanCellValue();   
  106.                                        
  107.                                        
  108.                                     cell.getDateCellValue();   
  109.                                        
  110.                                     break;   
  111.   
  112.                                 default:   
  113.                                 }   
  114.                                    
  115.                                 System.out.println(value);   
  116.   
  117.                             }   
  118.                         }   
  119.                     }   
  120.                 }   
  121.             }   
  122.         } catch (Exception e) {   
  123.   
  124.             e.printStackTrace();   
  125.         }   
  126.   
  127.     }   
  128.   
  129.     /**  
  130.      * @param args  
  131.      * @throws Exception   
  132.      */  
  133.     public static void main(String[] args) throws Exception {   
  134.         // TODO Auto-generated method stub  
  135.   
  136.         File f = new File("d:/test.xlsx");   
  137.            
  138.         FileInputStream is = new FileInputStream(f);   
  139.            
  140.         System.out.println(f.getName());   
  141.            
  142.         read(is,false);   
  143.            
  144.            
  145.     }   
  146.   
  147. }  

 

原创粉丝点击