解析excel

113 阅读1分钟
File file=new File("c:/Users/Administrator/Desktop/aaa.xls");
try {
    //创建Excel,读取文件内容
    HSSFWorkbook workbook=new HSSFWorkbook(FileUtils.openInputStream(file));
    //获取Sheet方式一
    // sheet=workbook.getSheet("Sheet0");
    //获取sheet方式二 下标
    HSSFSheet sheet=workbook.getSheetAt(0);
    //读取第一行
    int firstRowNum=0;
    int lastRowNum=sheet.getLastRowNum();
    for(int i=0;i<=lastRowNum;i++){
        HSSFRow row=sheet.getRow(i);
        int lastCellNum=row.getLastCellNum();
        for(int j=0;j<lastCellNum;j++){
            row.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING);
            HSSFCell cell=row.getCell(j);
            String value=cell.getStringCellValue();
            System.out.print(value+" ");
        }
        System.out.println();
    }

} catch (IOException e) {
    e.printStackTrace();
}