Iterate through the collection of WorksheetCell objects and get a cell text value.
The following example shows how to extract cells from Microsoft Office Excel spreadsheet:
// Create an instance of Parser class
try(Parserparser=newParser(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(WorksheetInfoi: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(WorksheetCellc: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();}}}
The following example shows how to extract cells from Microsoft Office Excel spreadsheet with customization:
// Create an instance of Parser class
try(Parserparser=newParser(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
WorksheetInfoinfo=parser.getWorksheetInfo(0);// Print the worksheet name
System.out.println(info.getName());System.out.println();// Create the range that represents the first two rows
WorksheetRangerange=newWorksheetRange(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,newWorksheetOptions(range));// Iterate over cells
for(WorksheetCellc: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:
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.