GetWorksheetCells(int, WorksheetOptions) - returns a collection of cells contained in set ranges.
Here are the steps to extract cells from Microsoft Office Excel spreadsheet:
Instantiate Parser object for the initial spreadsheet;
Call GetWorksheetCells(int) method and obtain the collection of objects;
Iterate through the collection of WorksheetCell and get a cell text value.
The following example shows how to extract cells from Microsoft Office Excel spreadsheet.
// Create an instance of Parser classusing(Parserparser=newParser(Constants.SampleXlsx)){// Check if worksheet cells extraction is supportedif(!parser.Features.Worksheet){thrownewNotSupportedException("Worksheet cells extraction isn't supported");}// Get the information about worksheetsIEnumerable<WorksheetInfo>info=parser.GetWorksheetInfo();// Iterate over worksheet informationforeach(WorksheetInfoiininfo){// Print the worksheet nameConsole.WriteLine(i.Name);Console.WriteLine();// Get the worksheet cellsIEnumerable<WorksheetCell>cells=parser.GetWorksheetCells(i.Index);// Iterate over cellsforeach(WorksheetCellcincells){// Print the cell information and text valueConsole.WriteLine($"Row: {c.RowIndex} Column: {c.ColumnIndex} RowSpan: {c.RowSpan} ColumnSpan: {c.ColumnSpan}");Console.WriteLine(c.Text);Console.WriteLine();}}}
The following example shows how to extract cells from Microsoft Office Excel spreadsheet with customization:
// Create an instance of Parser classusing(Parserparser=newParser(Constants.SampleXlsx)){// Check if worksheet cells extraction is supportedif(!parser.Features.Worksheet){thrownewNotSupportedException("Worksheet cells extraction isn't supported");}// Get the information about the first worksheetWorksheetInfoinfo=parser.GetWorksheetInfo(0);// Print the worksheet nameConsole.WriteLine(info.Name);Console.WriteLine();// Create the range that represents the first two rowsWorksheetRangerange=newWorksheetRange(info.MinRowIndex,Math.Min(info.MinRowIndex+1,info.MaxRowIndex),info.MinColumnIndex,info.MaxColumnIndex);// Get the worksheet cells from the first two rowsIEnumerable<WorksheetCell>cells=parser.GetWorksheetCells(0,newWorksheetOptions(range));// Iterate over cellsforeach(WorksheetCellcincells){// Print the cell information and text valueConsole.WriteLine($"Row: {c.RowIndex} Column: {c.ColumnIndex} RowSpan: {c.RowSpan} ColumnSpan: {c.ColumnSpan}");Console.WriteLine(c.Text);Console.WriteLine();}}
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 .NET 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.