javaexcelapache-poieclipse-oxygen

Java code not printing all rows from excel file


I have an excel file with 6663 rows. I want to read all the rows and columns in the excel file and print them out on my console in eclipse. Here is what I have tried to achieve this:

public class ExcelReader {
    public static final String SAMPLE_XLSX_FILE_PATH = "K:\\Documents\\Project\\Netword_GUI\\Netword_GUI\\src\\libs\\cc2017.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

        // Creating a Workbook from an Excel file (.xls or .xlsx)
        Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));

        // Retrieving the number of sheets in the Workbook
        System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");

        /*
           =============================================================
           Iterating over all the sheets in the workbook (Multiple ways)
           =============================================================
         */

        // You can obtain a sheetIterator and iterate over it
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        System.out.println("Retrieving Sheets using Iterator");
        while (sheetIterator.hasNext()) {
            Sheet sheet = sheetIterator.next();
            //System.out.println(sheet.getRow(0));
            System.out.println("=> " + sheet.getSheetName());
        }
        // Getting the Sheet at index zero
        Sheet sheet = workbook.getSheetAt(0);

        // Create a DataFormatter to format and get each cell's value as String
        DataFormatter dataFormatter = new DataFormatter();

        // You can obtain a rowIterator and columnIterator and iterate over them
        System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            // Now let's iterate over the columns of the current row
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t");
            }
            System.out.println();
        }
        if (sheet.getActiveCell() == null) {
            // Closing the workbook
            workbook.close();

        }
    }
}

The intention of this code is to display all the rows and columns. At the moment this code just shows roughly about 200 rows but all the columns for these rows are showing as intended. It also seems to be displaying the rows in a random order although, every time I run it the same rows show in the same random order. I would appreciate any solution in order to display all of the 6663 rows in the correct order including the headers (first row). Thank you in advance.


Solution

  • If you are running this inside Eclipse you are probably hitting the limit on the size of the Console output.

    You can change the limit in the Preferences in the 'Run/Debug > Console' page. You can change the maximum number of characters or turn off the limiting altogether (but this can lead to out of memory errors). enter image description here