Free Support Forum - aspose.com

ASPOSE.WORDS. Dynamic creation of Tables

I'd like to create dinamically many tables.

Could you please provide me with sample how to do that?

I tried to do that in following manner but failed and no example found on this site.

builder.Writeln();

// Adding header row of Summary Table

DataRow drHeader = tblSummary.Rows[0];

builder.Font.ClearFormatting();

builder.StartTable();

for (int j = 0; j < tblSummary.Columns.Count; j++)

{

builder.RowFormat.Alignment = RowAlignment.Center;

builder.InsertCell();

builder.Write(drHeader[j].ToString());

}

builder.EndRow();

builder.EndTable();

--... After that I'm adding new table like that:

builder.Font.ClearFormatting();

builder.StartTable();

for (int i = 0; i < tbl.Rows.Count; i++)

{

for (int j = 0; j < tbl.Columns.Count; j++)

{

builder.RowFormat.Alignment = RowAlignment.Center;

builder.InsertCell();

builder.Write(tbl.Rows[i][j].ToString());

}

builder.EndRow();

}

builder.EndTable();

But that code doesn't produce tables in Aspose.Word document.

Only 1 table can be buit successfully (without another one).

Hi Alex,

Refer to this thread here for tips on how to get started dynamically building a simple table in your document. Try that code and see if it generates a table in your document.

Also check that tblSummary.Columns.Count is returning a value > 0. This could be the cause of no cells appearing in the document.

Please feel free to ask if you have any problems.

Thanks,

Hi Adam!
The problem not in that I cannot build the table dynamically - but in that I cannot build 2 (and more) tables dynamically one after another.
Like that:
(these table just for example)

Table 1
------------------------------
| UserID | OrderID |
------------------------------
125 | 546879865436|
687 | 654683516546|
------------------------------|


Table 2
----------------------------------|

| UserName | Birthday Date |

----------------------------------|

Alex | 1980 |

Michael | 1979 |
----------------------------------|


The problem may be caused by:
builder.StartTable();
builder.EndRow();
builder.EndTable();
I’m not sure that I use them correctly.

I can build table and add rows, but how to do that for many tables and add them into 1 word doc. ?

Please, provide me with example.
The last one you pointed didn’t help me.



Hi Alex,

Please try the following code. It creates 2 tables in one document.

Document doc = new Document();<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln(@"Table1");

// Create first table

builder.InsertCell();

builder.Write(@"UserID");

builder.InsertCell();

builder.Write(@"OrderID");

builder.EndRow();

builder.InsertCell();

builder.Write(@"125");

builder.InsertCell();

builder.Write(@"546879865436");

builder.EndRow();

builder.InsertCell();

builder.Write(@"687");

builder.InsertCell();

builder.Write(@"654683516546");

builder.EndRow();

builder.EndTable();

builder.Writeln();

builder.Writeln(@"Table2");

// Create second table

builder.InsertCell();

builder.Write(@"UserName");

builder.InsertCell();

builder.Write(@"Birthday Date");

builder.EndRow();

builder.InsertCell();

builder.Write(@"Alex");

builder.InsertCell();

builder.Write(@"1980");

builder.EndRow();

builder.InsertCell();

builder.Write(@"Michael");

builder.InsertCell();

builder.Write(@"1979");

builder.EndRow();

builder.EndTable();

doc.Save(@"2tables.docx");

Regards,

Thank you for that example.

But my document logic is more coplex, that’s why I have a problems here.
Below two methods which add tables and chart into word.doc.
Could you please have a glance to check what is wrong there:

// This method is triggered in the loop - adding many tables in such a way

public static void AddTableToDocument(DataTable tbl, DataTable tblSummary, int iQuestionNum, ref DocumentBuilder builder, Stream chartStream, string sQuestionText)
{
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.TopMargin = 1;

builder.InsertBreak(BreakType.PageBreak);

// Adding Question Text
string sCompleteQuestionText = "Question No. " + iQuestionNum.ToString() + ": ";
sCompleteQuestionText += sQuestionText;
builder.Font.Bold = true;
builder.Font.Size = 10;
builder.Writeln(sCompleteQuestionText);
// Adding Question Text. End

//AddSummaryTable(ref tblSummary, ref iQuestionNum, ref builder);

// Adding Distribution Table
builder.Font.ClearFormatting();
//builder.StartTable();

for (int i = 0; i < tbl.Rows.Count; i++)
{
for (int j = 0; j < tbl.Columns.Count; j++)
{
builder.RowFormat.Alignment = RowAlignment.Center;
builder.InsertCell();
builder.Write(tbl.Rows[i][j].ToString());

}
builder.EndRow();
}

builder.EndTable();

if (chartStream != null)
{
builder.Writeln();

if(chartStream.Length != 10802)
{
builder.InsertImage(chartStream);
}
else
{
// TODO: CHANGE ABLOSUTE PATHS IN WHOLE SOLUTION

}
}
builder.MoveToDocumentEnd();
}


// Adding onothe one table


private static void AddSummaryTable(ref DataTable tblSummary, ref int iQuestionNum, ref DocumentBuilder builder)
{
builder.Writeln();
// Adding header row of Summary Table
DataRow drHeader = tblSummary.Rows[0];
builder.Font.ClearFormatting();

for (int j = 0; j < tblSummary.Columns.Count; j++)
{
builder.RowFormat.Alignment = RowAlignment.Center;
builder.InsertCell();
builder.Write(drHeader[j].ToString());
}

builder.EndRow();
builder.EndTable();
// builder.Writeln();

// Adding header row of Summary Table. End

//string selectExpression = "QNumber = " + “’” + iQuestionNum.ToString() + “’”;
//DataRow[] aRow = tblSummary.Select(selectExpression);

//if (aRow.Length > 0)
//{
// builder.Font.ClearFormatting();
// builder.StartTable();

// for (int j = 0; j < tblSummary.Columns.Count; j++)
// {
// builder.RowFormat.Alignment = RowAlignment.Center;
// builder.InsertCell();
// builder.Write(aRow[0][j].ToString());
// }

// builder.EndRow();
// builder.EndTable();
//}
}

Thank you for that example.

But my document logic is more coplex, that’s why I have a problems here.
Below two methods which add tables and chart into word.doc.
Could you please have a glance to check what is wrong there:

// This method is triggered in the loop - adding many tables in such a way

public static void AddTableToDocument(DataTable tbl, DataTable tblSummary, int iQuestionNum, ref DocumentBuilder builder, Stream chartStream, string sQuestionText)
{
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.TopMargin = 1;

builder.InsertBreak(BreakType.PageBreak);

// Adding Question Text
string sCompleteQuestionText = "Question No. " + iQuestionNum.ToString() + ": ";
sCompleteQuestionText += sQuestionText;
builder.Font.Bold = true;
builder.Font.Size = 10;
builder.Writeln(sCompleteQuestionText);
// Adding Question Text. End

AddSummaryTable(ref tblSummary, ref iQuestionNum, ref builder);

// Adding Distribution Table
builder.Font.ClearFormatting();
//builder.StartTable();

for (int i = 0; i < tbl.Rows.Count; i++)
{
for (int j = 0; j < tbl.Columns.Count; j++)
{
builder.RowFormat.Alignment = RowAlignment.Center;
builder.InsertCell();
builder.Write(tbl.Rows[i][j].ToString());

}
builder.EndRow();
}

builder.EndTable();

if (chartStream != null)
{
builder.Writeln();

if(chartStream.Length != 10802)
{
builder.InsertImage(chartStream);
}
else
{
// TODO: CHANGE ABLOSUTE PATHS IN WHOLE SOLUTION

}
}
builder.MoveToDocumentEnd();
}


// Adding onothe one table


private static void AddSummaryTable(ref DataTable tblSummary, ref int iQuestionNum, ref DocumentBuilder builder)
{
builder.Writeln();
// Adding header row of Summary Table
DataRow drHeader = tblSummary.Rows[0];
builder.Font.ClearFormatting();

for (int j = 0; j < tblSummary.Columns.Count; j++)
{
builder.RowFormat.Alignment = RowAlignment.Center;
builder.InsertCell();
builder.Write(drHeader[j].ToString());
}

builder.EndRow();
builder.EndTable();
// builder.Writeln();

// Adding header row of Summary Table. End

//string selectExpression = "QNumber = " + “’” + iQuestionNum.ToString() + “’”;
//DataRow[] aRow = tblSummary.Select(selectExpression);

//if (aRow.Length > 0)
//{
// builder.Font.ClearFormatting();
// builder.StartTable();

// for (int j = 0; j < tblSummary.Columns.Count; j++)
// {
// builder.RowFormat.Alignment = RowAlignment.Center;
// builder.InsertCell();
// builder.Write(aRow[0][j].ToString());
// }

// builder.EndRow();
// builder.EndTable();
//}
}

Hi Alex,

I'd like to clarify one thing. Are the tables that you create after the first table missing in the document completely or they just merged with the first table?

Thanks,

Tables are created and not merged with other tables.
I don’t use functionality in the meaning Aspose “merge”.

Please look at attached picture.
I need to put in Summary Table in word.doc - method AddSummaryTable
Method AddTableToDocument does its job right.
But when I call method AddSummaryTable within AddTableToDocument then problems start - word doc loose its structure and tables are not shown correctly.

Hi.


Could you attach a document where I can see incorrect tables?

Thanks,

Word.doc is attached

Hi,

Please see attached sample code. I took the code you posted before and made a modification to use string array instead of DataTable. The last page of the attached output.doc contains 2 tables added by my sample code. I don't see any issues with that.

Probably you use some old version of Aspose.Words.NET so please try the latest version which is 9.0.0 (http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx).

Regards,

I located where problem is -
in another two methods: BeginningDocument and BuildCoverPage
Could you please point out me exact place in those methods which causes the problem?

I attached Class which contains all methods (including above mentioned) for building word doc.
Thanks!

P.S. I use Aspose.Words for .NET 9.0.0

Hi,


I can’t run the code you attached because it uses external data and images. Could you create a simple test application that would help me to reproduce the issue on my side? Please attach the test application and the output document that this application generates.

Thanks,