Extract cells from Microsoft Office Excel spreadsheets

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-parser -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-parser/parser-net-mcp:latest
Folders, licensing and more →

GroupDocs.Parser provides the functionality to extract cells from spreadsheets. The following methods are used for this purpose:

To check whether the document supports this functionality, use the isWorksheet method of the Features class. If cells extraction isn’t supported, getWorksheetCells and getWorksheetInfo(int) methods return null.

Here are the steps to extract cells from Microsoft Office Excel spreadsheet:

The following example shows how to extract cells from Microsoft Office Excel spreadsheet:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Check if worksheet cells extraction is supported
    if (!parser.getFeatures().isWorksheet()) {
        System.out.println("Worksheet cells extraction isn't supported.");
        return;
    }

    // Get the information about worksheets
    Iterable<WorksheetInfo> info = parser.getWorksheetInfo();

    // Iterate over worksheet information
    for (WorksheetInfo i : info) {
        // Print the worksheet name
        System.out.println(i.getName());
        System.out.println();

        // Get the worksheet cells
        Iterable<WorksheetCell> cells = parser.getWorksheetCells(i.getIndex());

        // Iterate over cells
        for (WorksheetCell c : cells) {
            // Print the cell information and text value
            System.out.println(String.format("Row: %d Column: %d RowSpan: %d ColumnSpan: %d",
                    c.getRowIndex(), c.getColumnIndex(), c.getRowSpan(), c.getColumnSpan()));
            System.out.println(c.getText());
            System.out.println();
        }
    }
}

WorksheetInfo contains the worksheet index and name and the bounds of the data on the worksheet (getMinRowIndex, getMaxRowIndex, getMinColumnIndex, getMaxColumnIndex). Use these values to build a WorksheetRange and pass it to WorksheetOptions to extract only the cells you need. The WorksheetOptions constructor accepts one or more ranges.

The following example shows how to extract cells from Microsoft Office Excel spreadsheet with customization:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Check if worksheet cells extraction is supported
    if (!parser.getFeatures().isWorksheet()) {
        System.out.println("Worksheet cells extraction isn't supported.");
        return;
    }

    // Get the information about the first worksheet
    WorksheetInfo info = parser.getWorksheetInfo(0);

    // Print the worksheet name
    System.out.println(info.getName());
    System.out.println();

    // Create the range that represents the first two rows
    WorksheetRange range = new WorksheetRange(
            info.getMinRowIndex(),
            Math.min(info.getMinRowIndex() + 1, info.getMaxRowIndex()),
            info.getMinColumnIndex(),
            info.getMaxColumnIndex());

    // Get the worksheet cells from the first two rows
    Iterable<WorksheetCell> cells = parser.getWorksheetCells(0, new WorksheetOptions(range));

    // Iterate over cells
    for (WorksheetCell c : cells) {
        // Print the cell information and text value
        System.out.println(String.format("Row: %d Column: %d RowSpan: %d ColumnSpan: %d",
                c.getRowIndex(), c.getColumnIndex(), c.getRowSpan(), c.getColumnSpan()));
        System.out.println(c.getText());
        System.out.println();
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.