从excel获取数据的问题(使用Java)

时间:2015-01-29 13:26:20

标签: java excel

我在调用两次时从这个函数获取数据时遇到问题。该函数返回从Excel工作表中提取的所有行的arrayList。当我第一次调用函数时,我得到正确的行数(除了标题行和带退出的行之外的所有行)。我第二次调用函数时得到0.

似乎文件或第二次创建的工作表发生了一些事情,这里是代码:

private static List<String[]> getDataFromXLS(String excelPath) {

    FileInputStream fis;
    Workbook workbook; Sheet sheet; XSSFRow row;
    Iterator<Row> rows; 
    XSSFCell cell; 
    List<String[]> allExcelRows = new ArrayList<String[]>();
    String[] xlsRow;
    columnNames = new LinkedHashMap<Integer, String>();
    paramNames = new LinkedHashMap<String, Integer>();
    int totalColumnCount = 0;
    int rowNumber = 1;

    try {
        fis = new FileInputStream(new File(excelPath));
        workbook = WorkbookFactory.create(fis);
        sheet = workbook.getSheet("TestData");
        rows = sheet.rowIterator();

        while (rows.hasNext()) {        

            row = ((XSSFRow) rows.next());
            if (rowNumber == 1) {
                //based on amount of parameters on first xls row
                totalColumnCount = row.getLastCellNum();
            }

            xlsRow = new String[totalColumnCount];

            //check which column is TestType
            //iterate through all the columns
            for (int columnNumber=0; columnNumber<totalColumnCount; columnNumber++) {

                cell = row.getCell(columnNumber, Row.CREATE_NULL_AS_BLANK); 
                if (getCellValue(cell).trim().toLowerCase().trim().equals("testtype") ){
                    testTypeColumnIndex = columnNumber; //this is Testtype index
                    break;
                }
            }

            if (rowNumber != 1) {                   
                for(int columnNumber=0; columnNumber<totalColumnCount; columnNumber++) {
                    cell = row.getCell(columnNumber, Row.CREATE_NULL_AS_BLANK);                    

                    //read only rows before exit
                    if (columnNumber == testTypeColumnIndex && getCellValue(cell).trim().toLowerCase().trim().equals("exit") ){
                        reachedExit = true;
                        break;
                    }
                    xlsRow[columnNumber] = getCellValue(cell).trim();
                }

                //reached exit?
                if (reachedExit) {
                    break; 
                }
                allExcelRows.add(xlsRow);
            } else {
                //save column names into map
                for(int columnNumber=0; columnNumber<totalColumnCount; columnNumber++) {
                    cell = row.getCell(columnNumber, Row.CREATE_NULL_AS_BLANK);                                                                     
                    columnNames.put(columnNumber, getCellValue(cell).trim());
                    paramNames.put(getCellValue(cell).trim(), columnNumber);
                }
            }
            rowNumber++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    fis.close();
    return allExcelRows;
}

1 个答案:

答案 0 :(得分:0)

我在这里做了一些猜测,但我认为问题是在方法开始时没有重置reachedExit类级布尔值。因此,当您第二次执行此代码块时调用它:

//reached exit?
if (reachedExit) {
    break; 
}

....意味着没有任何内容被添加到allExcelRows