GroupDocs.Parser provides the functionality to extract tables from documents including Excel spreadsheets (XLS, XLSX), Word documents, PDFs, and other supported formats by the getTables(PageTableAreaOptions) method:
GroupDocs.Parser supports extracting structured table data from Excel spreadsheets. This is especially useful for processing data-rich Excel files programmatically.
For spreadsheets, getTables() (or getTables(PageTableAreaOptions) without a table layout) returns the Excel tables defined in the workbook (ranges formatted as a table) - one PageTableArea per table. The page index of each table (getPage.getIndex()) is the index of the worksheet that contains it. Use getTables(int) to extract tables from a single worksheet. Plain cell ranges that are not formatted as tables are not returned; use worksheet cells extraction for them.
Basic steps
Here are the basic steps to extract tables from an Excel spreadsheet:
Iterate through the collection and access table cells.
Example: extract tables from Excel spreadsheet
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleXlsx)){// Check if table extraction is supported
if(!parser.getFeatures().isTables()){System.out.println("Table extraction is not supported for this Excel file.");return;}// Extract all tables from the Excel spreadsheet
Iterable<PageTableArea>tables=parser.getTables();inttableIndex=0;for(PageTableAreatable:tables){System.out.println(String.format("=== Table %d (worksheet %d) ===",++tableIndex,table.getPage().getIndex()));System.out.println(String.format("Rows: %d, Columns: %d",table.getRowCount(),table.getColumnCount()));// Iterate over rows
for(introw=0;row<table.getRowCount();row++){// Iterate over columns
for(intcolumn=0;column<table.getColumnCount();column++){// Get the table cell
PageTableAreaCellcell=table.getCell(row,column);if(cell!=null){// Print the cell text
System.out.print(cell.getText()+" | ");}}System.out.println();}}}
Extract tables with custom layout
When you know the exact table structure (columns and rows positions), pass it as TemplateTableLayout in PageTableAreaOptions. Here are the steps to extract tables from the whole document:
Instantiate Parser object for the initial document;
Iterate through the collection and print table cells.
The following example shows how to extract tables from the whole document:
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleInvoicePagesPdf)){// Check if the document supports table extraction
if(!parser.getFeatures().isTables()){System.out.println("Document isn't supports tables extraction.");return;}// Create the layout of tables
TemplateTableLayoutlayout=newTemplateTableLayout(java.util.Arrays.asList(newDouble[]{50.0,95.0,275.0,415.0,485.0,545.0}),java.util.Arrays.asList(newDouble[]{325.0,340.0,365.0,395.0}));// Create the options for table extraction
PageTableAreaOptionsoptions=newPageTableAreaOptions(layout);// Extract tables from the document
Iterable<PageTableArea>tables=parser.getTables(options);// Iterate over tables
for(PageTableAreat:tables){// Iterate over rows
for(introw=0;row<t.getRowCount();row++){// Iterate over columns
for(intcolumn=0;column<t.getColumnCount();column++){// Get the table cell
PageTableAreaCellcell=t.getCell(row,column);if(cell!=null){// Print the table cell text
System.out.print(cell.getText());System.out.print(" | ");}}System.out.println();}System.out.println();}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: