读取Excel文件信息到数组

//poi-ooxml poi
public String[] readExcel(File file) throws Exception{
        InputStream inputStream = new FileInputStream(file);  
        String fileName = file.getName();  
        List<String> list = new ArrayList<String>();
        Workbook wb = null;  
        if(fileName.endsWith("xls")){  
            wb = new HSSFWorkbook(inputStream);//解析xls格式  
        }else if(fileName.endsWith("xlsx")){  
            wb = WorkbookFactory.create(inputStream);//解析xlsx格式  
        }  
        Sheet sheet = wb.getSheetAt(0);//第一个工作表
        int firstRowIndex = sheet.getFirstRowNum()+1;  
        int lastRowIndex = sheet.getLastRowNum();
        for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){  
            Row row = sheet.getRow(rIndex);  
            if(row != null){  
                Cell cell = row.getCell(0);  
                String value = "";  
                if(cell != null && !"".equals(cell.toString())){  
                    value = cell.toString(); 
                    list.add(value);
                }  
            }  
        }
        String[] staticType = list.toArray(new String[list.size()]);
        return staticType;
    }


你可能感兴趣的:(读取Excel文件信息到数组)