Extract text from Microsoft Office Excel spreadsheets
Extract text from Microsoft Office Excel spreadsheets
Leave feedback
On this page
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
Method
Use Case
Performance
Output Quality
Whole Document
Extract all text at once
Fast
Standard
Sheet-by-Sheet
Process individual worksheets
Medium
Standard
Raw Mode
High-speed bulk processing
Fastest
Lower formatting accuracy
Formatted Text
Preserve formatting (HTML/Markdown)
Slower
Highest
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:
Instantiate Parser object for the initial spreadsheet
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(Parserparser=newParser(Constants.SampleXlsx)){// Extract a text into the reader
try(TextReaderreader=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:
Instantiate Parser object for the initial spreadsheet
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleXlsx)){// Get the spreadsheet info
IDocumentInfospreadsheetInfo=parser.getDocumentInfo();// Iterate over sheets
for(intp=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(TextReaderreader=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.
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleXlsx)){// Get the spreadsheet info
IDocumentInfospreadsheetInfo=parser.getDocumentInfo();// Iterate over sheets
for(intp=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(TextReaderreader=parser.getText(p,newTextOptions(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:
Instantiate Parser object for the initial spreadsheet
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleXlsx)){// Extract a formatted text into the reader
try(TextReaderreader=parser.getFormattedText(newFormattedTextOptions(FormattedTextMode.Html))){// Print a formatted text from the spreadsheet
System.out.println(reader.readToEnd());}}
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.