Java代码无法打印Excel文件中的所有行

3

我有一个包含6663行的Excel文件。我想读取Excel文件中的所有行和列,并在Eclipse控制台上打印出来。以下是我尝试过的方法:

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();

        }
    }
}

这段代码的意图是显示所有行和列。目前,这段代码只显示了大约200行,但所有这些行的列都按预期显示。它似乎也以随机顺序显示行,尽管每次运行时相同的行都以相同的随机顺序显示。我希望能够提供任何解决方案,以便按正确的顺序(包括标题行)显示所有6663行。谢谢您提前。

我也查看了堆大小,但这并没有使用完所有可用的堆空间。 - David Gardener
可能是如何使用Apache POI加载大型xlsx文件?的重复问题。 - mallikarjun
1
没有,我有21列。 - David Gardener
你能附上文件或者一个展示相同问题的较小文件吗? - jmarkmurphy
1
如果@greg-449的答案不是解决方案,那么你希望Iterator也有完全空的行吗?它不会。但是其他情况下:Excel文件cc2017.xlsx从哪里来?使用了什么软件创建它?以及使用了什么apache poi版本?早期的apache poi版本只读取设置了行号的行。 如果是这样,XML看起来像<row r="3" ...>。但是属性r是可选的,某些第三方软件可能不总是设置它。早期的apache poi版本无法在没有行号的情况下读取这些行。 - Axel Richter
1个回答

2
如果您在Eclipse中运行此程序,可能会遇到控制台输出大小限制的问题。
您可以在“运行/调试>控制台”页面的首选项中更改限制。您可以更改最大字符数或完全关闭限制(但这可能会导致内存不足错误)。 enter image description here

什么鬼?我从未使用过IDE,但我认为IDE是为了支持程序员而制作的。但是,我在这里阅读有关IDE问题的更多信息后,得出的结论是完全相反的。我的意思是,甚至一些堆栈跟踪也可能达到该限制。 - Axel Richter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接