Free Support Forum - aspose.com

Issue with Merge Cells

hi,


I need merge the frist column tableCells into only one tableCell in the table which is after binding data in the office-Word.


dt.TableName = TName + item.F_CLASS;
myDoc.MailMerge.ExecuteWithRegions(dt);

How to achieve?

Hi,


Thanks for your inquiry. I have attached a couple of input/output sample documents here for your reference. The following code copies the values of first column in Table, merge these values with the values in second column and then pasts the merged text in the second column:

Document doc = new
Document(@“C:\Temp\in.docx”);

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

doc.MailMerge.ExecuteWithRegions(GetDataTable());

doc.Save(@"C:\Temp\out.docx");


private class HandleMergeField : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (args.TableName.Equals("region") && args.FieldName.Equals("cell2"))

{

Cell cell = (Cell)args.Field.Start.GetAncestor(NodeType.Cell);

if (cell != null)

{

Cell prevCell = (Cell)cell.PreviousSibling;

if (prevCell != null)

{

string mergedText = prevCell.Range.Text + args.FieldValue.ToString();

DocumentBuilder builder = new DocumentBuilder(args.Document);

builder.MoveToMergeField(args.FieldName);

builder.Write(mergedText);

}

}

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

}

}

private static DataTable GetDataTable()

{

DataTable dataTable = new DataTable("region");

dataTable.Columns.Add(new DataColumn("cell1", typeof(string)));

dataTable.Columns.Add(new DataColumn("cell2", typeof(string)));

DataRow dataRow;

for (int i = 0; i < 100; i++)

{

dataRow = dataTable.NewRow();

dataRow["cell1"] = "First Cell" + i;

dataRow["cell2"] = "Second cell" + i;

dataTable.Rows.Add(dataRow);

}

return dataTable;

}


I hope, this helps.

Best Regards,

Hi,


Please let us know any time you have any further queries. We’re always glad to help you.

Best Regards,

Hi,

I test the code,but failed,

and I want merged all the first column cells to only one cell

like































aa



cc



cc



bb



bb



dd



dd

Hi,


Thanks for your inquiry. Please attach your input and target Word documents here for testing. I will investigate as to how you are expecting your final document to be generated like. You can use Microsoft Word to create your target Word document. I will then provide you code to achieve what you’re looking for.

Best Regards,

here the file,thank you

Hi,


Please attach your target Word document here for testing as well. I will investigate as to how you are expecting your final document to be generated like. You can use Microsoft Word to create your target Word document. I will then provide you code to achieve what you’re looking for.

Moreover, I believe, you’ll find the following article helpful in case you want the first column cells be merged together into a single cell:
http://www.aspose.com/docs/display/wordsnet/Working+with+Merged+Cells

Best Regards,