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.
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)
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();
//}
}
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();
//}
}
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?
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.
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.
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!
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.