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 23, 2017 9:58 am

Hi
I try to export one gridview in asp.net to word document, but I had no luck. Please look at my code:
Code: Select all
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using Spire.Doc.Documents;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Web.UI.WebControls;

Code: Select all
Document doc = new Document();
        //------
        Section s = doc.AddSection();
        Paragraph Paras = s.AddParagraph();
        Paras.AppendText("Este é um bom exemplo");
        Paras.AppendText("linha seguinte");
        Paragraph lixo = s.AddParagraph();
        lixo.AppendText("");

        Spire.Doc.Table table = s.AddTable(true);
        int k;
       
        //Create Header and Data
        String[] Header = { "Item", "Description", "Qty", "Unit Price", "Price" };
        String[][] data = {
      for (k = 0; k < 2; k++)
        {
            new String[] { GridView1.Rows[k].Cells[0].Text, GridView1.Rows[k].Cells[1].Text, GridView1.Rows[k].Cells[2].Text, GridView1.Rows[k].Cells[3].Text, GridView1.Rows[k].Cells[4].Text },
        }                       
    };
    //Add Cells
        table.ResetCells(data.Length + 1, Header.Length);
        table.TableFormat.Borders.Horizontal.LineWidth = 0;
        table.TableFormat.Borders.Vertical.LineWidth = 0;
        //Header Row
       Spire.Doc.TableRow FRow = table.Rows[0];
        FRow.IsHeader = true;
        //Row Height
        FRow.Height = 23;
        //Header Format
       // FRow.RowFormat.BackColor = Color.AliceBlue;
        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.Center;
            //Data Format
            TextRange TR = p.AppendText(Header[i]);
            TR.CharacterFormat.FontName = "Calibri";
            TR.CharacterFormat.FontSize = 14;
            //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;

            //C Represents Column.
            for (int c = 0; c < data[r].Length; 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.Center;
                TR2.CharacterFormat.FontName = "Calibri";
                TR2.CharacterFormat.FontSize = 12;
                //TR2.CharacterFormat.TextColor = Color.Brown;
            }
        }

        //Save and Launch
        doc.SaveToFile("c:\\editorialportugal\\WordTable.docx", FileFormat.Docx2013);
        System.Diagnostics.Process.Start("c:\\editorialportugal\\WordTable.docx");


It seems I can't use the following code:
Code: Select all
 String[][] data = {
      for (k = 0; k < 2; k++)
        {
            new String[] { GridView1.Rows[k].Cells[0].Text, GridView1.Rows[k].Cells[1].Text, GridView1.Rows[k].Cells[2].Text, GridView1.Rows[k].Cells[3].Text, GridView1.Rows[k].Cells[4].Text },
        }                       
    };

Any help, please?
thank you

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

Tue Oct 24, 2017 3:58 am

Hello,

Thanks for your inquiry.
There's something wrong with your code. Please see below.
Code: Select all
         String[][] data = new String[2][];

            for (int k = 0; k < 2;k++ )
            {
                data[k] = new string[this.dataGridView1.ColumnCount];
                for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                {
                   
                    data[k][j] = this.dataGridView1.Rows[k].Cells[j].Value.ToString();
                }     
            }


Sincerely,
Jane
E-iceblue support team
User avatar

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

Tue Oct 24, 2017 9:11 am

Thank you Jane
Solved!

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

Tue Oct 24, 2017 9:15 am

Hello mariolopes,

Thanks for your quick response.
Please feel free to contact us if you need any help.

Sincerely,
Jane
E-iceblue support team
User avatar

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

Sun Apr 18, 2021 6:34 pm

Hi,

I am using a doc template to fill some fields from my windows form application. Along with that i am also copying a datagridview using the code you have given here.
This code works fine. But the table is created in an additional page of the word document. Is there a way i can add this table on the same page just below the existing template contents ?
Thanks for your support in advance.

thanks and regards
Mani

mani8051
 
Posts: 2
Joined: Sun Apr 18, 2021 6:30 pm

Mon Apr 19, 2021 8:47 am

Hello Mani,

Thanks for your inquiry.
Please refer to the following modified code to meet your needs. If there are still any issues, please provide us with your input file and your desired output for reference. Thanks in advance.
Code: Select all
            …
            //Section s = doc.AddSection();
            Section s = doc.Sections[0];
            …


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Apr 20, 2021 3:02 pm

thanks Brian.. it worked exactly how i wanted it to. thanks again for your support

Regards
Mani

mani8051
 
Posts: 2
Joined: Sun Apr 18, 2021 6:30 pm

Wed Apr 21, 2021 1:18 am

You are welcome.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc