Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Aug 08, 2017 8:22 pm

Hi,

Is there are any way to dynamically generate a table with vertical headings from the template?
I'm trying to create something like this:
Image
1. Summary should be predefined.
2. I have a collection of web sites.

Thanks.

TrueRohan
 
Posts: 3
Joined: Mon Jun 26, 2017 6:15 pm

Wed Aug 09, 2017 7:58 am

Hello,

Please use the following code to get approach to it.
Code: Select all
  Document doc = new Document();
            Section section = doc.AddSection();
            string tabelName = "Table1";
           
            String[] firstRw = { "Summary", "R1C2", "R1C3", "R1C4" };
            String[] header = { "DailyUnique", "UniqueVisitors","Browser","Operation System" };

            Table table = section.AddTable(true);
            table.ResetCells(header.Length+1, firstRw.Length);

            //horizental header
            for (int i = 0; i < firstRw.Length;i++ )
            {
               TextRange TR= table.Rows[0].Cells[i].AddParagraph().AppendText(firstRw[i]);
               TR.CharacterFormat.FontName = "Calibri";
               TR.CharacterFormat.FontSize = 14;
               TR.CharacterFormat.TextColor = Color.BlueViolet;
               TR.CharacterFormat.Bold = true;
            }

            //vertical header
            for (int j = 1; j < header.Length+1;j++ )
            {
                TextRange TR = table.Rows[j].Cells[0].AddParagraph().AppendText(header[j-1]);
                TR.CharacterFormat.FontName = "Calibri";
                TR.CharacterFormat.FontSize = 14;
                TR.CharacterFormat.TextColor = Color.BlueViolet;
                TR.CharacterFormat.Bold = true;
            }

            //Data Row dynamically
            for (int r = 0; r < table.Rows.Count-1; r++)
            {
                TableRow DataRow = table.Rows[r + 1];

                //Row Height
                DataRow.Height = 20;

                //C Represents Column.
                for (int c = 1; c < table.Rows[r].Cells.Count; c++)
                {
                    //Cell Alignment
                    DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    //Fill Data in Rows
                    Paragraph p2 = DataRow.Cells[c].AddParagraph();
                    TextRange TR2 = p2.AppendText("xxxx");
                    //Format Cells
                    p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                    TR2.CharacterFormat.FontName = "Calibri";
                    TR2.CharacterFormat.FontSize = 12;
                    TR2.CharacterFormat.TextColor = Color.Brown;
                }
            }
            doc.SaveToFile("123456.docx");

If there's still any doubt, welcome to write back.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Aug 09, 2017 9:25 am

Thank you for the code.
So there is no way to do this from the doc template with mergeFields?

TrueRohan
 
Posts: 3
Joined: Mon Jun 26, 2017 6:15 pm

Wed Aug 09, 2017 10:20 am

Hello,

Thanks for your response.
Could you please share your template so we can have a better investigation?

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Aug 09, 2017 11:04 am

Please see attached.

TrueRohan
 
Posts: 3
Joined: Mon Jun 26, 2017 6:15 pm

Thu Aug 10, 2017 6:01 am

Hello,

Thanks for sharing the file.
Please refer to the following code.
Code: Select all
private void MergeField(object sender, EventArgs e)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Property\Property.docx");
             DataTable tb = CreateTable();

            doc.MailMerge.Execute(tb);
            doc.IsUpdateFields = true;
            string output = @"C:\Users\Administrator\Desktop\Property\11322.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);

        }
        private DataTable CreateTable()
        {
            DataTable tb = new DataTable();
            tb.TableName = "TestVerticalTable";
            tb.Columns.Add("Parameter");
            tb.Columns.Add("p1Min");
            tb.Columns.Add("p2Min");
            tb.Columns.Add("p3Min");
            tb.Columns.Add("p4Min");
            tb.Columns.Add("p5Min");
            tb.Columns.Add("p6Min");
            tb.Columns.Add("p7Min");
            tb.Columns.Add("p8Min");
            tb.Columns.Add("p9Min");
            tb.Columns.Add("p10Min");
            tb.Columns.Add("p11Min");
            tb.Columns.Add("p12Min");
            tb.Columns.Add("p13Min");
            tb.Columns.Add("p14Min");

            tb.Columns.Add("p1Max");
            tb.Columns.Add("p2Max");
            tb.Columns.Add("p3Max");
            tb.Columns.Add("p4Max");
            tb.Columns.Add("p5Max");
            tb.Columns.Add("p6Max");
            tb.Columns.Add("p7Max");
            tb.Columns.Add("p8Max");
            tb.Columns.Add("p9Max");
            tb.Columns.Add("p10Max");
            tb.Columns.Add("p11Max");
            tb.Columns.Add("p12Max");
            tb.Columns.Add("p13Max");
            tb.Columns.Add("p14Max");
           
            tb.Rows.Add("Testing", "Testp1Min", "Testp2Min", "Testp3Min", "Testp4Min", "Testp5Min",
                "Testp6Min", "Testp7Min", "Testp8Min", "Testp9Min", "Testp10Min", "Testp11Min",
                "Testp12Min", "Testp13Min", "Testp14Min", "Testp1Max", "Testp2Max", "Testp3Max",
                "Testp4Max","Testp5Max","Testp6Max","Testp7Max","Testp8Max","Testp9Max",
                "Testp10Max","Testp11Max","Testp12Max","Testp13Max","Testp14Max");

            return tb;
        }

If there's still any doubt, welcome to write it back.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Aug 16, 2017 8:19 am

Hello,

How is the issue now?
Your feedback will be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc