I had a Word document that contains data for several products. Each product contains a specification table, and I want to create a windows application to search for product name and extract its table data using c#.
using (var doc = WordprocessingDocument.Open(@"C:\reader\Sample2xml.xml", false))
{
// To create a temporary table
DataTable dt = new DataTable();
int rowCount = 0;
// Find the first table in the document.
Table table = doc.MainDocumentPart.Document.Elements<Table>().First();
// To get all rows from table
IEnumerable<TableRow> rows = table.Elements<TableRow>();
// To read data from rows and to add records to the temporary table
foreach (TableRow row in rows)
{
if (rowCount == 0)
{
foreach (TableCell cell in row.Descendants<TableCell>())
{
dt.Columns.Add(cell.InnerText);
}
rowCount += 1;
}
else
{
dt.Rows.Add();
int i = 0;
foreach (TableCell cell in row.Descendants<TableCell>())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.InnerText;
i++;
}
}
}
What I need to do is to search for product name and then start range and get first table but it gives me error.