GroupDocs.Parser provides the functionality to extract tables from documents including Excel spreadsheets (XLS, XLSX), Word documents, PDFs, and other supported formats using the GetTables(PageTableAreaOptions) method.
Extract Tables from Excel C#
To extract tables from Excel C# applications, GroupDocs.Parser supports extracting structured table data from Excel spreadsheets. This is especially useful for processing data-rich Excel files programmatically.
Iterate through the collection and access table cells.
Example: Extract Tables from Excel Spreadsheet
usingGroupDocs.Parser;usingGroupDocs.Parser.Options;usingGroupDocs.Parser.Data;usingGroupDocs.Parser.Templates;// Excel file pathstringexcelPath="sample.xlsx";using(Parserparser=newParser(excelPath)){// Check if table extraction is supported for Excelif(!parser.Features.Tables){Console.WriteLine("Table extraction is not supported for this Excel file.");return;}// Extract all tables from the Excel spreadsheet// Pass null to extract tables automaticallyIEnumerable<PageTableArea>tables=parser.GetTables(null);inttableIndex=0;foreach(PageTableAreatableintables){Console.WriteLine($"\n=== Table {++tableIndex} ===");Console.WriteLine($"Rows: {table.RowCount}, Columns: {table.ColumnCount}");// Iterate over rowsfor(introw=0;row<table.RowCount;row++){// Iterate over columnsfor(intcolumn=0;column<table.ColumnCount;column++){// Get the table cellPageTableAreaCellcell=table[row,column];if(cell!=null){// Print the cell textConsole.Write($"{cell.Text}\t");}}Console.WriteLine();}}}
Example: Extract Tables with Custom Layout
The following example shows how to extract tables from documents when you know the exact table structure (columns and rows positions):
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Check if the document supports table extractionif(!parser.Features.Tables){Console.WriteLine("Document isn't supports tables extraction.");return;}// Create the layout of tablesTemplateTableLayoutlayout=newTemplateTableLayout(newdouble[]{50,95,275,415,485,545},newdouble[]{325,340,365,395});// Create the options for table extractionPageTableAreaOptionsoptions=newPageTableAreaOptions(layout);// Extract tables from the documentIEnumerable<PageTableArea>tables=parser.GetTables(options);// Iterate over tablesforeach(PageTableAreatintables){// Iterate over rowsfor(introw=0;row<t.RowCount;row++){// Iterate over columnsfor(intcolumn=0;column<t.ColumnCount;column++){// Get the table cellPageTableAreaCellcell=t[row,column];if(cell!=null){// Print the table cell textConsole.Write(cell.Text);Console.Write(" | ");}}Console.WriteLine();}Console.WriteLine();}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: