0

我正在开发一种用于上传 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("-");
            }
        }
    }

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

任何人都可以帮助我克服这个问题。

4

1 回答 1

0

这是一个给你的例子

 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() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
        }
        System.out.println("");
    }
于 2013-05-22T09:32:25.817 回答