java POI(二)

name.xslx

 

 

 

 1 public class Demo6 {
 2 
 3     public static void main(String[] args) throws IOException {
 4         InputStream is = new FileInputStream("F:\\poi\\name.xls");
 5         POIFSFileSystem fs = new POIFSFileSystem(is);
 6         HSSFWorkbook wb = new HSSFWorkbook(fs);
 7         HSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet页
 8         if(sheet==null){
 9             return;
10         }
11         //遍历row
12         for(int rowNum = 0; rowNum<=sheet.getLastRowNum();rowNum++ ){
13             HSSFRow row = sheet.getRow(rowNum);
14             if(row == null){
15                 continue;
16             }
17             //遍历cell
18             for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
19                 HSSFCell cell = row.getCell(cellNum);
20                 if(cell == null){
21                     continue;
22                 }
23                 System.out.println(" ");
24             }
25             System.out.println();
26         }
27     }
28 
29     private static String getValue(HSSFCell cell){
30         String cellvalue = "";
31         switch (cell.getCellType()){
32             case HSSFCell.CELL_TYPE_NUMERIC:
33                     cellvalue = String.valueOf(cell.getNumericCellValue());
34                 break;
35             case HSSFCell.CELL_TYPE_STRING:
36                     cellvalue = String.valueOf(cell.getStringCellValue());
37                 break;
38             case HSSFCell.CELL_TYPE_BOOLEAN:
39                     cellvalue = String.valueOf(cell.getBooleanCellValue());
40                 break;
41             case HSSFCell.CELL_TYPE_FORMULA:
42                     cellvalue = String.valueOf(cell.getArrayFormulaRange());
43                 break;
44             default:
45                 break;
46         }
47         return cellvalue;
48     }
49 }

 

Exception:
            The supplied data appears to be in the Office 2007+ XML. 


关键问题的原因还是excel2003和excel2007版本的问题
3、解决办法
(1)判断文件后缀名是xls,还是xlsx
(2)如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook    

 疑问:解析excel还有很多个版本吗????

转载于:https://www.cnblogs.com/hk-zsg/p/11530961.html

你可能感兴趣的:(java POI(二))