Excel文件读取逐行值



我正在开发一个上传excel并验证它的工具,但excel文件太大,行数约为65536。

这里是我的代码用于上传和读取excel表值

    int firstColNo = 1;
    int rowcount = sheet.getRows();
    int colCount = sheet.getColumns();
    int row = 0;
    String comp = "";
    for (row = 1; row < rowcount; row++) {
        if (labelCell != null) {
            cell = sheet.getCell(firstColNo, row);
            if (cell.getContents() != null && cell.getContents().length() > 0){
                String compoundId = cell.getContents();               
                System.out.println(compoundId);
            } else {
                System.out.println("-");
            }
        }
    }

通过读取行值,它需要更多的时间来读取,有没有办法使它更快,否则任何代码修改需要在我的代码中完成?

谁能帮我解决这个问题?

给你一个例子

 HSSFWorkbook workbook = new HSSFWorkbook(file);
    //Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "tt");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "tt");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "tt");
                    break;
            }
        }
        System.out.println("");
    }

最新更新