1 while reading downloaded xlsx file in Java

I am trying to read a xlsx file with particular row number which will be provided as parameter.

I am getting NullPointerException if sheet.getRow(RowNum). I am able to read the xlsx file if i adjust column width and save it again manually. But that destroys my purpose of automation. I am able to read any other xlsx files which are created manually.

Here is the sample code :

public String readCouponCode(int getRowCount) {

try {
    Registration reg=new Registration();
    File inputFile = new File(this.DownloadFile);
    System.out.println(DownloadFile);
    // Get the workbook instance for XLSX file
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));

    // Get first sheet from the workbook
    XSSFSheet sheet = wb.getSheetAt(0);

    // Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = sheet.iterator();

    // Traversing over each row of XLSX file

    Row row = sheet.getRow(getRowCount);
    // For each row, iterate through each columns
    Iterator<Cell> cellIterator = row.cellIterator();
    while (cellIterator.hasNext()) {

        Cell cell = cellIterator.next();
        int cellIndex = cell.getColumnIndex();
        // System.out.println(cellIndex);
        if (cellIndex == 0) {
            CouponCode = cell.getStringCellValue();
            System.out.println(cell.getStringCellValue() + "t");
        }
    }
} catch (Exception e) {
    System.err.println("Exception :" + e.getMessage());
}
return CouponCode;

} I also tried with XSSFRow but it yields same result.

Note : I tried by commenting sheet.getRow() line and it prints only the last row. I tried to get number of rows by using sheet.getPhysicalNumberOfRows(), it gives 1 but actually my xlsx file has 7 rows.

Jars used:

dom4j poi-3.13-20150929 poi-excelant-3.13-20150929 poi-ooxml-3.13-20150929 poi-ooxml-schemas-3.13-20150929 xmlbeans-2.5.0

链接地址: http://www.djcxy.com/p/37980.html

上一篇: XLSX获得最后一列?

下一篇: 1,同时阅读Java下载的xlsx文件