Free Support Forum - aspose.com

Is it Possible with Aspose.Word

I am creating an application that will read the Ms word file which will have a table with rows and columns. i want to get all the data from the Ms word Table and update the Database Fields . Similarly I want to Generate a document report with a table of records. Can i achieve this functionality with aspose.word

Hi

Thanks for your inquiry. You can get data from word table and the work with this data. For example see the following code. This code converts a word table into DataTable.

Document doc = new Document(@"333_101395_faraz\in.doc");

Table table = doc.FirstSection.Body.Tables[0];

DataTable dt = GetDataTable(table);

///

/// convert word table into Datatable

///

///

/// DataTable

public DataTable GetDataTable(Table table)

{

DataTable dt = new DataTable();

char[] escArr = { '\a' };

foreach (Cell cell in table.FirstRow.Cells)

{

dt.Columns.Add(cell.Range.Text.Trim(escArr));

}

for (int i = 1; i < table.Rows.Count; i++)

{

DataRow row = dt.NewRow();

for (int j = 0; j < table.Rows[i].Cells.Count; j++)

{

row[j] = table.Rows[i].Cells[j].Range.Text.Trim(escArr);

}

dt.Rows.Add(row);

}

return dt;

}

The word document contains the following table.

ID

FirstName

LastName

City

1

Den

Johnson

New York

2

Andy

Osborn

Auckland

3

Adrian

Gibbs

London

4

Luke

Shelton

Liverpool

Also you can insert DataTable into the word document using the following code.

Document doc1 = new Document();

DocumentBuilder builder = new DocumentBuilder(doc1);

InsertTable(builder, dt);

doc1.Save(@"333_101395_faraz\out.doc");

///

/// Insert Table to document

///

public void InsertTable(DocumentBuilder builder, DataTable table)

{

builder.StartTable();

foreach (DataColumn col in table.Columns)

{

builder.InsertCell();

builder.Write(col.ColumnName);

}

builder.EndRow();

foreach (DataRow row in table.Rows)

{

foreach (object cell in row.ItemArray)

{

builder.InsertCell();

builder.Write(cell.ToString());

}

builder.EndRow();

}

builder.EndTable();

}

I hope that this will help you to solve your task.

Best regards.