3

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.

David Gardener
  • 93
  • 2
  • 13
  • I also looked at heap sizes but this does not use up all the heap space available. – David Gardener Jul 19 '18 at 09:49
  • Possible duplicate of [How to load a large xlsx file with Apache POI?](https://stackoverflow.com/questions/11891851/how-to-load-a-large-xlsx-file-with-apache-poi) – mallikarjun Jul 19 '18 at 09:52
  • 1
    No I have 21 columns – David Gardener Jul 19 '18 at 10:03
  • Can you attach the file, or a smaller one that exhibits the same issues? – jmarkmurphy Jul 19 '18 at 11:06
  • 1
    It the answer from @greg-449 is not the solution, are you expecting the `Iterator` has totally empty rows too? It will not. But else: Where is the Excel file `cc2017.xlsx` coming from? What software has created it? And what `apache poi`version is used? Former `apache poi` versions had only read rows if they had a row number set. The XML looks like `` if so. But the attribute `r` is optional and some third party software may not always set it. Former `apache poi` versions was unable to read such rows without row number. – Axel Richter Jul 19 '18 at 12:30

1 Answers1

2

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

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • What the heck? I never used IDEs but I thought that IDEs were made to support programmers. But the more I read here about problems with IDEs I come to the conclusion that the exact opposite is true. I mean even some stacktraces may hit that limitation. – Axel Richter Jul 19 '18 at 10:30