Extract text from Microsoft Office Excel spreadsheets

Overview

This guide demonstrates how to extract text content from Microsoft Office Excel spreadsheets (.xls, .xlsx) using the GroupDocs.Parser for Java API. You’ll learn different text extraction methods suitable for various document processing scenarios, from simple Excel text retrieval to sheet-by-sheet parsing.

Extraction Methods Comparison

MethodUse CasePerformanceOutput Quality
Whole DocumentExtract all text at onceFastStandard
Sheet-by-SheetProcess individual worksheetsMediumStandard
Raw ModeHigh-speed bulk processingFastestLower formatting accuracy
Formatted TextPreserve formatting (HTML/Markdown)SlowerHighest

Method 1: Extract Text from Entire Spreadsheet

When to use: When you need all text content from the Excel workbook and don’t need to distinguish between different worksheets.

To extract text from Microsoft Office Excel spreadsheets, the getText method is used. This method retrieves content from the entire document.

Steps:

  1. Instantiate Parser object for the initial spreadsheet
  2. Call getText method and obtain TextReader object
  3. Read text from the reader
Warning
getText method returns null value if text extraction isn’t supported for the document. For example, text extraction isn’t supported for Zip archive. Therefore, for Zip archive getText method returns null. For empty Microsoft Office Excel spreadsheets getText method returns an empty TextReader object (readToEnd method returns an empty string).

Example:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Extract a text into the reader
    try (TextReader reader = parser.getText()) {
        // Print a text from the spreadsheet
        System.out.println(reader.readToEnd());
    }
}

Method 2: Extract Text from Individual Sheets

When to use: When you need to process each Excel worksheet separately, maintain sheet organization, or perform sheet-specific data extraction.

This method uses getText(int) to extract text from specific sheets. Each worksheet is treated as a separate page of the document.

Steps:

  1. Instantiate Parser object for the initial spreadsheet
  2. Call getDocumentInfo method and obtain IDocumentInfo object with getPageCount method
  3. Call getText(int) method with the sheet index and obtain TextReader object
  4. Read text from the reader

Example:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Get the spreadsheet info
    IDocumentInfo spreadsheetInfo = parser.getDocumentInfo();

    // Iterate over sheets
    for (int p = 0; p < spreadsheetInfo.getPageCount(); p++) {
        // Print a sheet number
        System.out.println(String.format("Sheet %d/%d", p + 1, spreadsheetInfo.getPageCount()));

        // Extract a text into the reader
        try (TextReader reader = parser.getText(p)) {
            // Print a text from the spreadsheet
            System.out.println(reader.readToEnd());
        }
    }
}

Method 3: High-Speed Raw Text Extraction

When to use: When processing large Excel files or many spreadsheets where parsing speed is more important than formatting accuracy. Suitable for Excel data mining, content indexing, or bulk document processing.

Raw mode increases text extraction performance by sacrificing formatting accuracy. Use getText(TextOptions) and getText(int, TextOptions) methods for raw mode extraction.

Warning
Some spreadsheets may have different sheet numbers in raw and accurate modes. Use getRawPageCount instead of getPageCount in raw mode.

Steps:

  1. Instantiate Parser object for the initial spreadsheet
  2. Instantiate TextOptions object with true parameter
  3. Call getDocumentInfo method
  4. Use getRawPageCount instead of getPageCount to avoid extra calculations
  5. Call getText(int, TextOptions) method with the sheet index and obtain TextReader object
  6. Read text from the reader

Example:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Get the spreadsheet info
    IDocumentInfo spreadsheetInfo = parser.getDocumentInfo();

    // Iterate over sheets
    for (int p = 0; p < spreadsheetInfo.getRawPageCount(); p++) {
        // Print a sheet number
        System.out.println(String.format("Sheet %d/%d", p + 1, spreadsheetInfo.getRawPageCount()));

        // Extract a text into the reader
        try (TextReader reader = parser.getText(p, new TextOptions(true))) {
            // Print a text from the spreadsheet
            System.out.println(reader.readToEnd());
        }
    }
}

Method 4: Extract Formatted Text (HTML)

When to use: When you need to preserve the visual structure and formatting of Excel spreadsheet data, or when integrating extracted content into web applications or formatted reports.

GroupDocs.Parser allows extracting text from Microsoft Office Excel spreadsheets as HTML, Markdown and formatted plain text. For more details, see Extract Formatted Text.

Steps:

  1. Instantiate Parser object for the initial spreadsheet
  2. Call getFormattedText(FormattedTextOptions) method and obtain TextReader object
  3. Read text from the reader

Example:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleXlsx)) {
    // Extract a formatted text into the reader
    try (TextReader reader = parser.getFormattedText(new FormattedTextOptions(FormattedTextMode.Html))) {
        // Print a formatted text from the spreadsheet
        System.out.println(reader.readToEnd());
    }
}

Supported Excel Formats

  • .xls - Microsoft Excel 97-2003 Workbook
  • .xlsx - Microsoft Excel Open XML Workbook

Other spreadsheet formats (XLSM, XLSB, XLT, XLTX, XLTM, ODS, OTS, CSV, XLA, XLAM, NUMBERS) are listed in the Extract data from Microsoft Office Excel spreadsheets article.

Common Use Cases

Enterprise Document Processing

  • Excel Data Mining: Extract text for search indexing and content analysis
  • Spreadsheet Report Generation: Convert Excel workbook data to text-based reports
  • Content Migration: Move spreadsheet data between different document management systems

Business Intelligence & Analytics

  • Text Analytics on Excel Files: Analyze comments, notes, and text data within .xls/.xlsx spreadsheets
  • Compliance Document Processing: Extract text content for regulatory reporting from Excel documents
  • Audit Trails: Document text content changes over time in spreadsheet parsing workflows

Integration Scenarios

  • Enterprise Search Systems: Index Excel spreadsheet content for full-text search
  • Automated Data Pipelines: Extract text as part of automated document processing workflows
  • Content Management Systems: Process uploaded Excel files automatically in Java applications

Performance Considerations

File Size Impact

  • Small files (< 1MB): All methods perform similarly
  • Medium files (1-10MB): Raw mode provides noticeable speed improvement
  • Large files (> 10MB): Raw mode recommended for bulk processing

Memory Usage

  • Sheet-by-sheet processing uses less memory than whole document extraction
  • Raw mode is more memory-efficient for large files
  • Consider processing sheets individually for very large workbooks

Troubleshooting

Common Issues

Null TextReader Response

  • Verify the file format is supported (.xls, .xlsx)
  • Check if the file is corrupted or password-protected
  • Ensure the file path is correct and accessible

Empty Text Output

  • Confirm the spreadsheet contains text data (not just numbers/formulas)
  • Check if the sheets contain visible content
  • Verify the file isn’t completely empty

Performance Issues

  • Use raw mode for large files or bulk processing
  • Process sheets individually instead of extracting the entire document
  • Consider file size limitations in your environment

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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.