Received : 2007/07/12 14:11:53
Message : Is there a way to append tables to each other?
This message was posted using Aspose.Live 2 Forum
Received : 2007/07/12 14:11:53
Message : Is there a way to append tables to each other?
The answer lies in the structure of Aspose.Words object model, which in general reflects MS Word document format. In our document object model there is no notion of tables, there are only rows. Row nodes that follow one another are represented as one table in MS Word. So to append two existing tables, the row nodes of one table should be moved to a new place immediately following the rows of the other table. That can be done by removing nodes from their parent in one place using Node.Remove method and inserting them to another place using Node.Insert method.
I can come up with a code example if you will provide some sample documents for your task.
Best regards,
Here is the code I am working with. Essentially, I am building a client report that is not very data-base friendly. My first stab is to create a main document with the header row for table, then loop through the groups in the report and build tables from templates, appending each to the main document. I end up with a bunch of tables that show the data in the order the client requested (i've attached a sample document which was generated with the code below). What I really need is a document with one table (i.e, all of the tables merged into one in the order shown).
Thanks for the assistance.
private static string getSummaryDocReport(int iSurveyID, string strSurveyTitle, string strCompanyName, string strTemplatePath, string strStagingPath, string strOutputPath)
{
string strResponse = "";
try
{
string templatePath = "";
string strFileName = Path.Combine(strOutputPath, "Summary Document Report" + strSurveyTitle + "_" + strCompanyName + ".doc");
string strTableFile = Path.Combine(strStagingPath, "TempTable.doc");
CreateStarterDoc("Summary Document Report", strSurveyTitle, strCompanyName, strTemplatePath, strStagingPath);
string strStarter = Path.Combine(strStagingPath, "ReportStarter.doc"); //CreateStarterDoc creates the file ReportStarter.doc
Document docRCSReport = new Document(strStarter); //main doc with all pieces
templatePath = Path.Combine(strTemplatePath, "SummaryDocTableHeader.doc");
Document doc = new Document(templatePath);
doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
AppendDoc(docRCSReport, doc); //adds header row table
int iKAValue = 0;
int iSAL = 0;
int iKeyIndID = 0;
DataTable dtSAL = new DataTable();
DataTable dtKI = new DataTable();
DataTable dtKIDetails = new DataTable();
DataTable dtSum = new DataTable();
Document docDetails;
Document docSum;
DataTable dtKeyAtts = get_KeyAttValues(iSurveyID); //gets attributelevel values for survey
foreach (DataRow dr in dtKeyAtts.Rows) //loop keyatt
{
iKAValue = int.Parse(dr[0].ToString());
dtSAL.Clear();
dtSAL = getSubAttLevels(iSurveyID, iKAValue);
foreach (DataRow drSA in dtSAL.Rows) // loop subatt
{
iSAL = int.Parse(drSA[0].ToString());
dtKI.Clear();
dtKI = getKeyIndIDs(iSurveyID, iKAValue, iSAL);
foreach (DataRow drKI in dtKI.Rows) // loop keyind
{
iKeyIndID = int.Parse(drKI[0].ToString());
templatePath = Path.Combine(strTemplatePath, "KeyIndQuestionAndScoresTemplate.doc");
docDetails = new Document(templatePath);
docDetails.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
dtKIDetails.Clear();
dtKIDetails = getKIDocSummary(iSurveyID, iKAValue, iSAL, iKeyIndID);
dtKIDetails.TableName = "Questions";
docDetails.MailMerge.ExecuteWithRegions(dtKIDetails);
dtSum.Clear();
dtSum = getKeyIndSummary(iSurveyID, iKAValue, iSAL, iKeyIndID);
docDetails.MailMerge.Execute(dtSum);
docDetails.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
AppendDoc(docRCSReport, docDetails);
}
templatePath = Path.Combine(strTemplatePath, "SummaryDocSubAttRow.doc");
docSum = new Document(templatePath);
docSum.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
dtSum.Clear();
dtSum = getSubAttSummary(iSurveyID, iKAValue, iSAL);
docSum.MailMerge.Execute(dtSum);
docSum.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
AppendDoc(docRCSReport, docSum);
}
templatePath = Path.Combine(strTemplatePath, "SummaryDocKeyAttRow.doc");
docSum = new Document(templatePath);
docSum.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
dtSum.Clear();
dtSum = getKeyAttSummary(iSurveyID, iKAValue);
docSum.MailMerge.Execute(dtSum);
docSum.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
AppendDoc(docRCSReport, docSum);
}
templatePath = Path.Combine(strTemplatePath, "SummaryDocTotalRow.doc");
docSum = new Document(templatePath);
docSum.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
dtSum.Clear();
dtSum = getQuestionTotals(iSurveyID);
docSum.MailMerge.Execute(dtSum);
docSum.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
AppendDoc(docRCSReport, docSum);
docRCSReport.Save(strFileName);
strResponse = "Survey Summary Document Report Created!";
return strResponse;
}
catch
{
strResponse = "Failed to create Summary Document Report!";
return strResponse;
}
}
You are appending the whole documents contents, which are section with paragraph, table and paragraph. Try to import only table from these documents. It is basically the same code, just do ImportNode for the table, instead of section.
Hi Vladimir,
Can you provide a simple code example to get me started?
thanks
Well, it will be something like this:
private void AppendTableFromDoc(Document dstDoc, Document srcDoc)
{
// Extract only the first table from the source document
Table table = (Table)dstDoc.ImportNode(srcDoc.FirstSection.Body.Tables[0], true, ImportFormatMode.KeepSourceFormatting);
// Insert the table just before the last node in the destination document.
// We expect the previous table to be at the end of the document.
// But the document always ends with paragraph.
// So we need to insert the table just before the last paragraph in the destination document.
dstDoc.LastSection.Body.ChildNodes.Insert(dstDoc.LastSection.Body.Count - 1, table);
}
thanks so much for the quick reply. I had made a stab at a solution and ended up with one table, but it looked like I was appending the new tables randomly throughout the destination document. Your solution is perfect!