Excel 파일 정보를 그룹으로 읽기

1244 단어
//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;
    }

좋은 웹페이지 즐겨찾기