Open and read Excel file in Java

GemBox.Spreadsheet for Java can open many Excel file formats (including XLS, XLSX, CSV and ODS) in the same manner. Specifically, it uses a ExcelFile.Load method to read from an Excel file or stream in Java.

The following example demonstrates how to read all ExcelCells, their Value, and ValueType properties in an ExcelFile object.

Screenshot of Excel cell values read with GemBox.Spreadsheet for Java
Reading Excel Screenshot
Upload your file (Drag file here)
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = ExcelFile.load("%InputFileName%");

        StringBuilder sb = new StringBuilder();

        // Iterate through all worksheets in an Excel workbook.
        for (ExcelWorksheet worksheet : workbook.getWorksheets()) {
            sb.append('\n');
            sb.append(String.format("%1$s %2$s %1$s", "-----", worksheet.getName()));

            // Iterate through all rows in an Excel worksheet.
            for (ExcelRow row : worksheet.getRows()) {
                sb.append('\n');

                // Iterate through all allocated cells in an Excel row.
                for (ExcelCell cell : row.getAllocatedCells()) {
                    if (cell.getValueType() != CellValueType.NULL)
                        sb.append(String.format("%1$s [%2$s]           ", cell.getValue(), cell.getValueType()));
                    else
                        sb.append("                        ");
                }
            }
        }

        System.out.println(sb.toString());
    }
}

Note that, when you iterate through all read cells in an ExcelRow, you should use AllocatedCells collection to prevent an unnecessary allocation of ExcelCell objects.

Check next example or download examples from GitHub.