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

Table RowSpan and ColumnSpan

4 Answers 120 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.
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