Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Mar 18, 2014 2:37 pm

Hi,

I have a simple dataset with 3 tables:
Main (just parent entry)
Itemrows (rows belong to main entry)
Total (total belong to main entry)

If I merge these with a document (see down below), it only merges the Main and leaves the rest for what it is.

My document as the following setup:

«GroupStart:Main»«Description»

«TableStart:Itemrows»«Rowdesc»«RowPriceO»«Rowpricem»«TableEnd:Itemrows»
test
«TableStart:Total»«Rowpriceot»«Rowpricemt»«TableEnd:Total»

«GroupEnd:Main»

I fill the dataset mainly by simple code:

//Merge Calculation fields.
DataSet ds = new DataSet();

//Main entry
ds.Tables.Add("Main");
ds.Tables["Main"].Columns.Add("Id", typeof(int));
ds.Tables["Main"].Columns.Add("Description", typeof(string));

//Add one entry row
ds.Tables["Main"].Rows.Add();
ds.Tables["Main"].Rows[0]["Id"] = 1;
ds.Tables["Main"].Rows[0]["Description"] = "Main Entry";

//Item Rows
ds.Tables.Add("Itemrows");
ds.Tables["Itemrows"].Columns.Add("Id", typeof(int));
ds.Tables["Itemrows"].Columns.Add("Mainid", typeof(int));
ds.Tables["Itemrows"].Columns.Add("Rowdesc", typeof(String));
ds.Tables["Itemrows"].Columns.Add("Rowpriceo", typeof(decimal));
ds.Tables["Itemrows"].Columns.Add("Rowpricem", typeof(decimal));

ds.Tables["Itemrows"].Columns["Id"].AutoIncrement = true;

//Total
ds.Tables.Add("Total");
ds.Tables["Total"].Columns.Add("Id", typeof(int));
ds.Tables["Total"].Columns.Add("Mainid", typeof(int));
ds.Tables["Total"].Columns.Add("Rowpriceot", typeof(decimal));
ds.Tables["Total"].Columns.Add("Rowpricemt", typeof(decimal));

ds.Tables["Total"].Columns["Id"].AutoIncrement = true;

decimal Tot_PriceOnce = 0;
decimal Tot_PriceMonthly = 0;

foreach (DOCU_DocTextCalcItem rw in ListOfDocTextCalcList)
{
ds.Tables["Itemrows"].Rows.Add();
ds.Tables["Itemrows"].Rows[ds.Tables["Itemrows"].Rows.Count - 1]["Mainid"] = 1;
ds.Tables["Itemrows"].Rows[ds.Tables["Itemrows"].Rows.Count - 1]["Rowdesc"] = rw.Description_Value;
ds.Tables["Itemrows"].Rows[ds.Tables["Itemrows"].Rows.Count - 1]["Rowpriceo"] = rw.PriceOnce;
ds.Tables["Itemrows"].Rows[ds.Tables["Itemrows"].Rows.Count - 1]["Rowpricem"] = rw.PriceMonthly;

Tot_PriceOnce += rw.PriceOnce;
Tot_PriceMonthly += rw.PriceMonthly;
}

ds.Tables["Total"].Rows.Add();
ds.Tables["Total"].Rows[ds.Tables["Total"].Rows.Count - 1]["Mainid"] = 1;
ds.Tables["Total"].Rows[ds.Tables["Total"].Rows.Count - 1]["Rowpriceot"] = Tot_PriceOnce;
ds.Tables["Total"].Rows[ds.Tables["Total"].Rows.Count - 1]["Rowpricemt"] = Tot_PriceMonthly;

List<DictionaryEntry> DicList = new List<DictionaryEntry>
{
new DictionaryEntry("Main", String.Empty),
new DictionaryEntry("Itemrows", "Mainid = %Main.Id%"),
new DictionaryEntry("Total", "Mainid = %Main.Id%")
};

FinalDocument.MailMerge.ClearFields = false;
FinalDocument.MailMerge.HideEmptyGroup = false;
FinalDocument.MailMerge.HideEmptyParagraphs = false;

int mergedRowIndex = 0;
FinalDocument.MailMerge.MergeField += delegate(object sender, Spire.Doc.Reporting.MergeFieldEventArgs e)
{
if (e.TableName == "Main")
{
if (e.RowIndex > mergedRowIndex)
{
mergedRowIndex = e.RowIndex;

//insert page break symbol before the paragraph of current field
//InsertPageBreak(e.CurrentMergeField);
}
}
else if (e.TableName == "Detail" && e.FieldName == "Discount")
{
double discount = Convert.ToDouble(e.FieldValue) * 100;
e.Text = Convert.ToString(discount);
}
};

FinalDocument.MailMerge.ExecuteWidthNestedRegion(ds,DicList);


Can anyone give me a hint?

Thx a lot,

S

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Tue Mar 18, 2014 10:36 pm

anyone?

I've checked the dataset, it contains 3 filled tables with correct data.
The only problem is, that it is just using the first table in the merge.

Thx,

S

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Wed Mar 19, 2014 3:30 am

Hello,

Sorry for inconvenience as the different time zone.

Please confirm if these merged field are nested the table. And both TableStart and TableEnd must be in the same row.

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Mar 24, 2014 9:29 am

Hello,

Has the issue been resolved? Could you please give some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Mar 24, 2014 9:03 pm

Hi,

Thank you for your response and solution options.
Timeframe is different indeed.
However, I found where things go wrong.

Before I use the merge option, I copy several document parts into one document. In this process, somehow I loose the table.
So, if there is no table, there is no merge. I don't really understand why the table isn't copied. This is the function I use:

void AddWordDocToWordDoc(Document Original, ref Document TargetDoc,int sectionnumber)
{
foreach (Section sec in Original.Sections)
{
foreach (Paragraph par in sec.Paragraphs)
{
TargetDoc.Sections[sectionnumber].Paragraphs.Add((Paragraph)par.Clone());
}
}
}

If you have a solution, that would be great!

Thx again,

S

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Mon Mar 24, 2014 9:45 pm

Hi again,

More work is done. I need to "merge" sections (which includes tables).
I can clone sections, but I want to add paragraphs to one sections out of different documents including tables and the order of the items. Knowing that sections contain tables, my question is how to "merge" 2 sections into 1.

Any options for me?

Thx again,

S

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Mon Mar 24, 2014 10:09 pm

document.InsertTextFromFile() maybe? where can I find this, it's not in my spire.doc.

S

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Tue Mar 25, 2014 2:10 am

Hello,

Thanks for the reply. Sorry for inconvenience.
The InsertTextFromFile() method was added since Spire.Doc4.9.47, and it can merge two documents, but it doesn't merge 2 sections into 1. And there is a method to merge 2 sections into 1 for your reference as below.
Code: Select all
Document OriginalDoc1 = new Document();
OriginalDoc1.LoadFromFile("word1.docx");
Document OriginalDoc2 = new Document();
OriginalDoc2.LoadFromFile("word2.docx");
List<DocumentObject> elements = new List<DocumentObject>();
foreach (Section Orisec1 in OriginalDoc1.Sections)
            {
                foreach (DocumentObject element in Orisec1.Body.ChildObjects)
                {
                    elements.Add(element);
                }
            }
foreach (Section Orisec2 in OriginalDoc2.Sections)
            {
                foreach (DocumentObject element in Orisec2.Body.ChildObjects)
                {
                    elements.Add(element);
                }
            }
Document TargetDoc = new Document();
Section sec = TargetDoc.AddSection();
foreach (DocumentObject element in elements)
            {
                sec.Body.ChildObjects.Add(element.Clone());
            }
TargetDoc.SaveToFile("MergeSection.docx", FileFormat.Docx);

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Tue Mar 25, 2014 8:19 am

Works! Thx a lot!

My final procedure:

void AddWordDocToWordDoc(bool HeadNumbers,Document Original, ref Document TargetDoc, int SectionIndex)
{
List<DocumentObject> elements = new List<DocumentObject>();
foreach (Section Orisec1 in Original.Sections)
{
foreach (DocumentObject element in Orisec1.Body.ChildObjects)
{
elements.Add(element);
}
}

foreach (DocumentObject element in elements)
{
TargetDoc.Sections[SectionIndex].Body.ChildObjects.Add(element.Clone());
}
}

sjarl.kuyltjes@sienn.com
 
Posts: 8
Joined: Fri Feb 21, 2014 7:11 pm

Tue Mar 25, 2014 8:47 am

Thanks for your quick reply.
Glad to hear that.
If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc