Free Support Forum - aspose.com

How to create a Table having Cells spanning multiple Rows or Columns

Hi there,

I would like to create a table using Aspose.Words API which might have cells spanning more than one row or column. HTML code of these types of tables can be described as follows:

<td rowspan="2" width="30%">This is cell spans two rows

<td valign="top" width="45%">This is just a regular cell

<td valign="top" width="45%" colspan="2">This cell spans two columns

Second Row Cell number 1 Second Row Cell number 2 Second Row Cell number 3

This is the visual representation of the table:

This is cell spans two rows This is just a regular cell This cell spans two columns
Second Row Cell number 1 Second Row Cell number 2 Second Row Cell number 3

Now using Aspose.Words I can ofcourse create a table using insertCell() method of DocumentBuilder. I can set style/formatting to the content of the cells, but I could not find any method in any class like RowFormat or CellFormat using which I can write code to generate table like above. Please let me know if Aspose.Words have any solution to resolve this issue.

One more issue is with the width of a table cell. Is there anyways we can set the width of a table cell that is specified in percentage just like the above example using Aspose.Words? I can only find the method called setWidth from CellFormat class. But this only accepts double valued parameters in points. As I am trying to convert HTML to Word Document, I dont know what would be the exact width of my table cells in points. So please check this issue as well.

Thanks a lot.

Nafi Y Haque

Hi

Thanks for your inquiry. I think that you should use CellFormat.verticalMerge and CellFormat.HorizontalMerge. For example see the following code in C#.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup setup = builder.CurrentSection.PageSetup; //get width of page

double width = setup.PageWidth - setup.LeftMargin - setup.RightMargin;

builder.CellFormat.Borders.LineStyle = LineStyle.Double; //set borders linestyle

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //set vertical alignment

builder.InsertCell(); //insert first cell

builder.CellFormat.VerticalMerge = CellMerge.First; //merge cells verticaly

builder.CellFormat.Width = width * 0.3; //set width of cell = 30%

builder.Write("This is cell spans two rows"); //insert text

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.CellFormat.Width = width * 0.35; //set width of cell = 35%

builder.Write("This is just a regular cell");

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First; //merge cells horizontaly

builder.CellFormat.Width = width * 0.35 / 2; //set width of cell = 17.5%

builder.Write("This cell spans two columns");

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.Width = width * 0.35 / 2; //set width of cell = 17.5%

builder.EndRow(); //end firs row

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.Width = width * 0.3;

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.CellFormat.Width = width * 0.35;

builder.Write("Second Row Cell number 1");

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.Width = width * 0.35 / 2;

builder.Write("Second Row Cell number 2");

builder.InsertCell();

builder.CellFormat.Width = width * 0.35 / 2;

builder.Write("Second Row Cell number 3");

builder.EndRow();

builder.EndTable();

doc.Save("out.doc");

I hope that this will help you.

Best regards.