Hi,
I am building a template which utilizes the <<cellMerge - horz>>
I want my table so that it merges any blank cells with the cell that has a value. So far I have a table that has 5 columns, Col 4 and 5 are blank and this has merged as one blank cell rather then to Col2 which has a value. Is this possible?
@Kayleigh
To merge cells in a Word table using Aspose.Words, you can utilize the Cell.Merge
method. If you want to merge blank cells with a cell that contains a value, you will need to check each cell’s content and perform the merge accordingly.
Here’s a simple example in C# that demonstrates how to merge blank cells in a table with the first non-blank cell in the same row:
using Aspose.Words;
using Aspose.Words.Tables;
Document doc = new Document("YourDocument.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // Get the first table
foreach (Row row in table.Rows)
{
Cell firstNonBlankCell = null;
foreach (Cell cell in row.Cells)
{
if (!string.IsNullOrEmpty(cell.GetText().Trim()))
{
firstNonBlankCell = cell;
break;
}
}
if (firstNonBlankCell != null)
{
foreach (Cell cell in row.Cells)
{
if (cell != firstNonBlankCell && string.IsNullOrEmpty(cell.GetText().Trim()))
{
firstNonBlankCell.Merge(cell);
}
}
}
}
doc.Save("MergedCellsDocument.docx");
In this code:
- We load a Word document and access the first table.
- We iterate through each row to find the first non-blank cell.
- We then merge any blank cells in that row with the first non-blank cell.
Make sure to adjust the document path and table index as needed. This approach will help you achieve the desired merging behavior in your Word table.
@Kayleigh
A cellMerge
tag instructs LINQ Reporting Engine to merge cells with same textual contents. So, in order to use the tag in the described scenario, please use the same value for all cells to be merged instead of an empty value.