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 shows how you can read all ExcelCell objects, their values (ExcelCell.getValue), and CellValueType properties in an ExcelFile object.

Reading Excel Screenshot
Screenshot of Excel cell values read with GemBox.Spreadsheet for Java
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 ExcelRow.getAllocatedCells to prevent an unnecessary allocation of ExcelCell objects.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.