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.

Mon Oct 30, 2017 11:08 am

Hi
I need to create a table with differente column widths. Please look at my code
Code: Select all
Spire.Doc.Document doc = new Spire.Doc.Document();       
        doc.LoadFromFile(@"C:\editorialportugal\teste.docx");       
        Section s = doc.Sections[0];
        Paragraph Paras = s.AddParagraph();
        Paras.AppendText("Listagem de Clientes: ");
        Spire.Doc.Table table = s.AddTable(true);
        //Create Header and Data
        String[] Header = { "Nome", "Email", "Desconto" };
        String[][] data = new String[GridView1.Rows.Count][];
        System.Data.DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);               
        for (int k = 0; k < GridView1.Rows.Count; k++)
        {
            data[k] = new string[GridView1.Columns.Count];
            data[k][0] = Server.HtmlDecode(this.GridView1.Rows[k].Cells[1].Text);//cliente
            data[k][1] = this.GridView1.Rows[k].Cells[11].Text;//email
            data[k][2] = this.GridView1.Rows[k].Cells[8].Text;//id                                   
        }
       
        table.ResetCells(data.Length + 1, Header.Length);       
        Spire.Doc.TableRow FRow = table.Rows[0];
        FRow.IsHeader = true;
        //Row Height
        FRow.Height = 23;
        //Header Format       
        for (int i = 0; i < Header.Length; i++)
        {
            //Cell Alignment
            Paragraph p = FRow.Cells[i].AddParagraph();
            FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            p.Format.HorizontalAlignment = HorizontalAlignment.Left;
            //Data Format
            TextRange TR = p.AppendText(Header[i]);
            TR.CharacterFormat.FontName = "Calibri";
            TR.CharacterFormat.FontSize = 10;
            //TR.CharacterFormat.TextColor = Color.Teal;
            TR.CharacterFormat.Bold = true;
        }
        //Data Row
        for (int r = 0; r < data.Length; r++)
        {
            Spire.Doc.TableRow DataRow = table.Rows[r + 1];
            //Row Height
            DataRow.Height = 20;
            for (int c = 0; c < 3; c++)
            {
                //Cell Alignment
                DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                //Fill Data in Rows
                Paragraph p2 = DataRow.Cells[c].AddParagraph();
                TextRange TR2 = p2.AppendText(data[r][c]);
                //Format Cells
                p2.Format.HorizontalAlignment = HorizontalAlignment.Left;
                TR2.CharacterFormat.FontName = "Calibri";
                TR2.CharacterFormat.FontSize = 9;
                //TR2.CharacterFormat.TextColor = Color.Brown;                                 
            }
        }
        //Save and Launch
        doc.SaveToFile("c:\\editorialportugal\\WordTable.docx", FileFormat.Docx2013);
        System.Diagnostics.Process.Start("c:\\editorialportugal\\WordTable.docx"); 

With this code I create a table but all the columns have the same size. How can I fix the size of column 1, for instance?
Any help, please?
regards
mario

mariolopes
 
Posts: 9
Joined: Thu Oct 19, 2017 11:06 am

Tue Oct 31, 2017 4:07 am

Hello mario,

Thanks for your inquiry.
There are two ways to set the table column width. Please refer to the code below.
1. Set the width by percent.(I recommend this method because it is easier)
Code: Select all
Spire.Doc.Document doc = new Spire.Doc.Document();       
        doc.LoadFromFile(@"C:\editorialportugal\teste.docx");       
        Section s = doc.Sections[0];
        Paragraph Paras = s.AddParagraph();
        Paras.AppendText("Listagem de Clientes: ");
        Spire.Doc.Table table = s.AddTable(true);
        //Create Header and Data
        String[] Header = { "Nome", "Email", "Desconto" };
        String[][] data = new String[GridView1.Rows.Count][];
        System.Data.DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);               
        for (int k = 0; k < GridView1.Rows.Count; k++)
        {
            data[k] = new string[GridView1.Columns.Count];
            data[k][0] = Server.HtmlDecode(this.GridView1.Rows[k].Cells[1].Text);//cliente
            data[k][1] = this.GridView1.Rows[k].Cells[11].Text;//email
            data[k][2] = this.GridView1.Rows[k].Cells[8].Text;//id                                   
        }
       
        table.ResetCells(data.Length + 1, Header.Length);       
        Spire.Doc.TableRow FRow = table.Rows[0];
        FRow.IsHeader = true;
        //Row Height
        FRow.Height = 23;
        //Header Format       
        for (int i = 0; i < Header.Length; i++)
        {
            //Cell Alignment
            Paragraph p = FRow.Cells[i].AddParagraph();
            FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            p.Format.HorizontalAlignment = HorizontalAlignment.Left;
            //Data Format
            TextRange TR = p.AppendText(Header[i]);
            TR.CharacterFormat.FontName = "Calibri";
            TR.CharacterFormat.FontSize = 10;
            //TR.CharacterFormat.TextColor = Color.Teal;
            TR.CharacterFormat.Bold = true;
        }
        //Data Row
        for (int r = 0; r < data.Length; r++)
        {
            Spire.Doc.TableRow DataRow = table.Rows[r + 1];
            //Row Height
            DataRow.Height = 20;
            for (int c = 0; c < 3; c++)
            {
                if (c == 0)
                    {
                        //Set the first column width to occupy the 10% of the whole table width.
                        DataRow.Cells[c].SetCellWidth(10, CellWidthType.Percentage);
                    }

                //Cell Alignment
                DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                //Fill Data in Rows
                Paragraph p2 = DataRow.Cells[c].AddParagraph();
                TextRange TR2 = p2.AppendText(data[r][c]);
                //Format Cells
                p2.Format.HorizontalAlignment = HorizontalAlignment.Left;
                TR2.CharacterFormat.FontName = "Calibri";
                TR2.CharacterFormat.FontSize = 9;
                //TR2.CharacterFormat.TextColor = Color.Brown;                                 
            }
        }
        //Save and Launch
        doc.SaveToFile("c:\\editorialportugal\\WordTable.docx", FileFormat.Docx2013);
        System.Diagnostics.Process.Start("c:\\editorialportugal\\WordTable.docx"); 


2. Set the table column width to be a fixed value.
Kindly note when using this method, you need first set the table layout type to be fixed and all the cells shoud be given a value.
Code: Select all
    Spire.Doc.Document doc = new Spire.Doc.Document();       
            doc.LoadFromFile(@"C:\editorialportugal\teste.docx");       
            Section s = doc.Sections[0];
            Paragraph Paras = s.AddParagraph();
            Paras.AppendText("Listagem de Clientes: ");
            Spire.Doc.Table table = s.AddTable(true);
            table.TableFormat.LayoutType = LayoutType.Fixed;

            //Create Header and Data
            String[] Header = { "Nome", "Email", "Desconto" };
            String[][] data = new String[GridView1.Rows.Count][];
            System.Data.DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);               
            for (int k = 0; k < GridView1.Rows.Count; k++)
            {
                data[k] = new string[GridView1.Columns.Count];
                data[k][0] = Server.HtmlDecode(this.GridView1.Rows[k].Cells[1].Text);//cliente
                data[k][1] = this.GridView1.Rows[k].Cells[11].Text;//email
                data[k][2] = this.GridView1.Rows[k].Cells[8].Text;//id                                   
            }
           
            table.ResetCells(data.Length + 1, Header.Length);       
            Spire.Doc.TableRow FRow = table.Rows[0];
            FRow.IsHeader = true;
            //Row Height
            FRow.Height = 23;
            //Header Format       
            for (int i = 0; i < Header.Length; i++)
            {
                //Cell Alignment
                Paragraph p = FRow.Cells[i].AddParagraph();
                FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                p.Format.HorizontalAlignment = HorizontalAlignment.Left;
                //Data Format
                TextRange TR = p.AppendText(Header[i]);
                TR.CharacterFormat.FontName = "Calibri";
                TR.CharacterFormat.FontSize = 10;
                //TR.CharacterFormat.TextColor = Color.Teal;
                TR.CharacterFormat.Bold = true;
            }

            table.Rows[0].Cells[0].CellWidthType = CellWidthType.Point;
            table.Rows[0].Cells[0].Width = 50f;
            table.Rows[0].Cells[1].CellWidthType = CellWidthType.Point;
            table.Rows[0].Cells[1].Width = 100f;
            table.Rows[0].Cells[2].CellWidthType = CellWidthType.Point;
            table.Rows[0].Cells[2].Width = 150f;
            //Data Row
            for (int r = 0; r < data.Length; r++)
            {
                Spire.Doc.TableRow DataRow = table.Rows[r + 1];
                //Row Height
                DataRow.Height = 20;

                for (int c = 0; c < 3; c++)
                {
                  DataRow.Cells[c].CellWidthType = CellWidthType.Point;
                  if (c == 0)
                    {
                        DataRow.Cells[c].Width = 50f;
                    }
                    if (c == 1)
                    {
                        DataRow.Cells[c].Width = 100f;
                    }
                    if(c==2)
                    {
                        DataRow.Cells[c].Width = 150f;
                    }
                    //Cell Alignment
                    DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    //Fill Data in Rows
                    Paragraph p2 = DataRow.Cells[c].AddParagraph();
                    TextRange TR2 = p2.AppendText(data[r][c]);
                    //Format Cells
                    p2.Format.HorizontalAlignment = HorizontalAlignment.Left;
                    TR2.CharacterFormat.FontName = "Calibri";
                    TR2.CharacterFormat.FontSize = 9;
                    //TR2.CharacterFormat.TextColor = Color.Brown;                                 
                }
            }
            //Save and Launch
            doc.SaveToFile("c:\\editorialportugal\\WordTable.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("c:\\editorialportugal\\WordTable.docx");


If there's still any doubt, please write back.

Sincerley,
Jane
E-iceblue support team
User avatar

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

Tue Oct 31, 2017 1:09 pm

Great.
Thank you.

mariolopes
 
Posts: 9
Joined: Thu Oct 19, 2017 11:06 am

Wed Nov 01, 2017 1:35 am

Hi mario,

Thanks for your feedback.
Come back any time if you need further assitance.

Sincerely,
Jane
E-iceblue support team
User avatar

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

Return to Spire.Doc