excel文件读取行明智的值

时间:2013-05-22 09:19:38

标签: java excel jexcelapi

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

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

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分: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("");
    }