This is a migrated thread and some comments may be shown as answers.

Table RowSpan and ColumnSpan

4 Answers 963 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
zake
Top achievements
Rank 1
zake asked on 14 Nov 2018, 05:24 AM

I want to create a table in word document with header that having rowspan and columnspan.

please help me, because I had tried but the result is so messy. I have attach the table like I want. Tks

 

* note : I used Telerik Q2015

4 Answers, 1 is accepted

Sort by
0
zake
Top achievements
Rank 1
answered on 14 Nov 2018, 06:05 AM

This is my code

 

01.public static void TesGenDoc()
02.        {
03.            RadFlowDocument document = new RadFlowDocument();
04. 
05.            DocxFormatProvider provider = new DocxFormatProvider();
06.            RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
07. 
08.            string reportFilePathDir = @"..\tes.docx";
09.            // Start to Generate Monber Doc
10.            using (Stream output = new FileStream(reportFilePathDir, FileMode.OpenOrCreate))
11.            {
12.                //*WRITE COVER
13.                editor.CharacterFormatting.FontFamily.LocalValue = new ThemableFontFamily("Arial");
14.                editor.CharacterFormatting.FontSize.LocalValue = 14;
15.                editor.CharacterFormatting.FontWeight.LocalValue = FontWeights.Bold;
16.                 
17.                /*Table*/
18.                Style style = new Style("ok", StyleType.Table);
19.                style.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(System.Windows.Media.Colors.Black);
20.                style.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold;
21.                style.CharacterProperties.FontSize.LocalValue = 9;
22. 
23.                document.StyleRepository.Add(style);
24. 
25.                Table table = editor.InsertTable();
26.                table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
27.                 
28.                document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId);
29.                table.StyleId = BuiltInStyleNames.TableGridStyleId;
30. 
31.                //header
32.                TableRow rowHeader = table.Rows.AddTableRow();
33.                rowHeader.RepeatOnEveryPage = true;
34. 
35.                //number cell header
36.                TableCell cellNumberHeader = rowHeader.Cells.AddTableCell();
37.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
38.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
39. 
40.                cellNumberHeader = rowHeader.Cells.AddTableCell();
41.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
42.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
43. 
44.                cellNumberHeader = rowHeader.Cells.AddTableCell();
45.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
46.                cellNumberHeader.RowSpan = 1; cellNumberHeader.ColumnSpan = 3;
47. 
48.                cellNumberHeader = rowHeader.Cells.AddTableCell();
49.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
50.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
51. 
52.                cellNumberHeader = rowHeader.Cells.AddTableCell();
53.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
54.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
55. 
56.                TableRow rowHeader1 = table.Rows.AddTableRow();
57.                rowHeader1.RepeatOnEveryPage = true;
58. 
59.                TableCell c1 = rowHeader1.Cells.AddTableCell();
60.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
61.                c1 = rowHeader1.Cells.AddTableCell();
62.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
63.                c1 = rowHeader1.Cells.AddTableCell();
64.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
65. 
66.                TableRow rowHeader2 = table.Rows.AddTableRow();
67.                TableCell c2 = rowHeader2.Cells.AddTableCell();
68.                c2.Blocks.AddParagraph().Inlines.AddRun("");
69.                c2 = rowHeader1.Cells.AddTableCell();
70.                c2.Blocks.AddParagraph().Inlines.AddRun("");
71.                c2 = rowHeader1.Cells.AddTableCell();
72.                c2.Blocks.AddParagraph().Inlines.AddRun("");
73.                c2 = rowHeader1.Cells.AddTableCell();
74.                c2.Blocks.AddParagraph().Inlines.AddRun("");
75.                c2 = rowHeader1.Cells.AddTableCell();
76.                c2.Blocks.AddParagraph().Inlines.AddRun("");
77.                c2 = rowHeader1.Cells.AddTableCell();
78.                c2.Blocks.AddParagraph().Inlines.AddRun("");
79.                c2 = rowHeader1.Cells.AddTableCell();
80.                c2.Blocks.AddParagraph().Inlines.AddRun("");
81. 
82.                //*EXPORT TO WORD
83.                provider.Export(document, output);
84.            }
85.        }
0
zake
Top achievements
Rank 1
answered on 14 Nov 2018, 06:05 AM

This is my code

 

01.public static void TesGenDoc()
02.        {
03.            RadFlowDocument document = new RadFlowDocument();
04. 
05.            DocxFormatProvider provider = new DocxFormatProvider();
06.            RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
07. 
08.            string reportFilePathDir = @"..\tes.docx";
09.            // Start to Generate Monber Doc
10.            using (Stream output = new FileStream(reportFilePathDir, FileMode.OpenOrCreate))
11.            {
12.                //*WRITE COVER
13.                editor.CharacterFormatting.FontFamily.LocalValue = new ThemableFontFamily("Arial");
14.                editor.CharacterFormatting.FontSize.LocalValue = 14;
15.                editor.CharacterFormatting.FontWeight.LocalValue = FontWeights.Bold;
16.                 
17.                /*Table*/
18.                Style style = new Style("ok", StyleType.Table);
19.                style.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(System.Windows.Media.Colors.Black);
20.                style.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold;
21.                style.CharacterProperties.FontSize.LocalValue = 9;
22. 
23.                document.StyleRepository.Add(style);
24. 
25.                Table table = editor.InsertTable();
26.                table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
27.                 
28.                document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId);
29.                table.StyleId = BuiltInStyleNames.TableGridStyleId;
30. 
31.                //header
32.                TableRow rowHeader = table.Rows.AddTableRow();
33.                rowHeader.RepeatOnEveryPage = true;
34. 
35.                //number cell header
36.                TableCell cellNumberHeader = rowHeader.Cells.AddTableCell();
37.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
38.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
39. 
40.                cellNumberHeader = rowHeader.Cells.AddTableCell();
41.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
42.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
43. 
44.                cellNumberHeader = rowHeader.Cells.AddTableCell();
45.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
46.                cellNumberHeader.RowSpan = 1; cellNumberHeader.ColumnSpan = 3;
47. 
48.                cellNumberHeader = rowHeader.Cells.AddTableCell();
49.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
50.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
51. 
52.                cellNumberHeader = rowHeader.Cells.AddTableCell();
53.                cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = FontWeights.Bold; cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
54.                cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
55. 
56.                TableRow rowHeader1 = table.Rows.AddTableRow();
57.                rowHeader1.RepeatOnEveryPage = true;
58. 
59.                TableCell c1 = rowHeader1.Cells.AddTableCell();
60.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
61.                c1 = rowHeader1.Cells.AddTableCell();
62.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
63.                c1 = rowHeader1.Cells.AddTableCell();
64.                c1.Blocks.AddParagraph().Inlines.AddRun("Header");
65. 
66.                TableRow rowHeader2 = table.Rows.AddTableRow();
67.                TableCell c2 = rowHeader2.Cells.AddTableCell();
68.                c2.Blocks.AddParagraph().Inlines.AddRun("");
69.                c2 = rowHeader1.Cells.AddTableCell();
70.                c2.Blocks.AddParagraph().Inlines.AddRun("");
71.                c2 = rowHeader1.Cells.AddTableCell();
72.                c2.Blocks.AddParagraph().Inlines.AddRun("");
73.                c2 = rowHeader1.Cells.AddTableCell();
74.                c2.Blocks.AddParagraph().Inlines.AddRun("");
75.                c2 = rowHeader1.Cells.AddTableCell();
76.                c2.Blocks.AddParagraph().Inlines.AddRun("");
77.                c2 = rowHeader1.Cells.AddTableCell();
78.                c2.Blocks.AddParagraph().Inlines.AddRun("");
79.                c2 = rowHeader1.Cells.AddTableCell();
80.                c2.Blocks.AddParagraph().Inlines.AddRun("");
81. 
82.                //*EXPORT TO WORD
83.                provider.Export(document, output);
84.            }
85.        }
0
Tanya
Telerik team
answered on 16 Nov 2018, 04:31 PM
Hi Zake,

When adding cells with a column/row span bigger than 1, you should have in mind that they take the space for the sibling cells as well. In other words, if a cell has RowSpacing=2, it occupies the space in the table for the cell that should be on the next row as well and eliminates the possibility of adding such. Here is how I changed the code in order to achieve a result similar to the desired one:
//header
TableRow rowHeader = table.Rows.AddTableRow();
rowHeader.RepeatOnEveryPage = true;
 
//number cell header
TableCell cellNumberHeader = rowHeader.Cells.AddTableCell();
cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = System.Windows.FontWeights.Bold;
cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
 
cellNumberHeader = rowHeader.Cells.AddTableCell();
cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = System.Windows.FontWeights.Bold;
cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
 
cellNumberHeader = rowHeader.Cells.AddTableCell();
cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = System.Windows.FontWeights.Bold;
cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
cellNumberHeader.RowSpan = 1; cellNumberHeader.ColumnSpan = 3;
 
cellNumberHeader = rowHeader.Cells.AddTableCell();
cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = System.Windows.FontWeights.Bold;
cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
 
cellNumberHeader = rowHeader.Cells.AddTableCell();
cellNumberHeader.Blocks.AddParagraph().Inlines.AddRun("Header").FontWeight = System.Windows.FontWeights.Bold;
cellNumberHeader.Shading.BackgroundColor = new ThemableColor(System.Windows.Media.Colors.LightGray);
cellNumberHeader.RowSpan = 2; cellNumberHeader.ColumnSpan = 1;
 
TableRow rowHeader1 = table.Rows.AddTableRow();
rowHeader1.RepeatOnEveryPage = true;
 
TableCell c1 = rowHeader1.Cells.AddTableCell();
c1.Blocks.AddParagraph().Inlines.AddRun("Header");
c1 = rowHeader1.Cells.AddTableCell();
c1.Blocks.AddParagraph().Inlines.AddRun("Header");
c1 = rowHeader1.Cells.AddTableCell();
c1.Blocks.AddParagraph().Inlines.AddRun("Header");
 
TableRow rowHeader2 = table.Rows.AddTableRow();
TableCell c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");
c2 = rowHeader2.Cells.AddTableCell();
c2.Blocks.AddParagraph().Inlines.AddRun("");

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asha
Top achievements
Rank 1
commented on 30 Dec 2022, 01:19 PM

Hi, how to add two column in one tablecell.

                   TableRow firstRow = table.Rows.AddTableRow();
                    firstRow.RepeatOnEveryPage = true;

                    TableCell cell1 = firstRow.Cells.AddTableCell();
                    cell1.Blocks.AddParagraph().Inlines.AddRun(questions.Section);
                    cell1.RowSpan = 8; cell1.ColumnSpan = 1;

                    var cell2 = firstRow.Cells.AddTableCell();
                    //cell2.ColumnSpan = 2;
                    cell2.Blocks.AddParagraph().Inlines.AddRun(questionsData[0].QuestionText);

                    foreach (var item in questionsData.Skip(1))
                    {
                        TableRow rowHeader1 = tblList[6].Rows.AddTableRow();
                        rowHeader1.RepeatOnEveryPage = true;

                        TableCell c1 = rowHeader1.Cells.AddTableCell();
                        //c1.ColumnSpan = 2;
                        c1.Blocks.AddParagraph().Inlines.AddRun(item.QuestionText);
                    }

i want my QuestionandText cell into two columns. Please help me on this.

Anna
Telerik team
commented on 03 Jan 2023, 12:36 PM

Hello,

Do you mean that the first row needs to have two columns while the table itself has three, as it is in the screenshot? This is achieved by adding two cells to the row and setting the second one to have a ColumnSpan = 2. Here is a code sample which makes a table just as the one on the picture.

            RadFlowDocument document = new RadFlowDocument();
            var section = document.Sections.AddSection();

            Table table = new Table(document);
            section.Blocks.Add(table);

            TableRow firstRow = table.Rows.AddTableRow();
            TableCell cell11 = firstRow.Cells.AddTableCell();
            TableCell cell12 = firstRow.Cells.AddTableCell();
            cell12.ColumnSpan = 2;

            TableRow secondRow = table.Rows.AddTableRow();
            TableCell cell21 = secondRow.Cells.AddTableCell();
            TableCell cell22 = secondRow.Cells.AddTableCell();
            TableCell cell23 = secondRow.Cells.AddTableCell();
            cell21.RowSpan = 7;

            for (int i = 0; i < 6; i++)
            {
                TableRow row = table.Rows.AddTableRow();
                TableCell cell1 = row.Cells.AddTableCell();
                TableCell cell2 = row.Cells.AddTableCell();
            }

            TableRow ninthRow = table.Rows.AddTableRow();
            TableCell cell91 = ninthRow.Cells.AddTableCell();
            TableCell cell92 = ninthRow.Cells.AddTableCell();
            TableCell cell93 = ninthRow.Cells.AddTableCell();
            cell91.RowSpan = 2;

            TableRow tenthRow = table.Rows.AddTableRow();
            TableCell cell101 = tenthRow.Cells.AddTableCell();
            TableCell cell102 = tenthRow.Cells.AddTableCell();

            return document;

I hope I understood your scenario correctly, but please, let me know if this is not the case or if further clarification is needed.

Asha
Top achievements
Rank 1
commented on 10 Jan 2023, 05:04 PM

Hello Anna,

Thanks for your reply but that was not the case. Kindly help on the below issue.

I have one table in word document without header and i am trying to insert data into cell.

I want fix the height of the existing table and alignment of each cell value.

this is the code =>

 int tblCellIndex = 0;
                foreach (var item in model.stateCodes)
                {
                    tblList[4].Rows[0].Height = new TableRowHeight(HeightType.AtLeast, 4.0);
                    tblList[4].Rows[0].Cells[tblCellIndex].Blocks.AddParagraph().Inlines.AddRun(!string.IsNullOrEmpty(item.StateCode) ? item.StateCode : "");
                    tblCellIndex = tblCellIndex + 1;
                }

Anna
Telerik team
commented on 11 Jan 2023, 09:31 AM

Hello,

Would it be possible for you to prepare a sample project which shows what you have so far and open a support ticket with us? This way we'll be able to look into the matter in more detail and help you out. Thank you in advance. 

0
zake
Top achievements
Rank 1
answered on 27 Nov 2018, 03:05 AM

wow that is work for me,,,

thank a lot Tanya... :)

Tags
WordsProcessing
Asked by
zake
Top achievements
Rank 1
Answers by
zake
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or