Java 代码不打印 excel 文件中的所有行

Java code not printing all rows from excel file

我有一个包含 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 行,包括 headers (第一行)。提前谢谢你。

如果您在 Eclipse 中 运行 这个,您可能会达到控制台输出大小的限制。

您可以在 'Run/Debug > Console' 页面的首选项中更改限制。您可以更改最大字符数或完全关闭限制(但这可能会导致内存不足错误)。