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 Apr 05, 2021 8:12 am

Hi,

How can I export the selected rows of a datagridview to a docx file as table.

Also, each time run the code, a new page should be created where the table should be put and every page should have a header with a text which if fixed and a date which will have current date

DB_007
 
Posts: 36
Joined: Sat Sep 08, 2018 4:45 pm

Mon Apr 05, 2021 10:54 am

Hello,

Thanks for your inquiry!

Please refer to the following code to achieve your needs. If the code does not match your needs, please provide us with your expected output file and source file for reference. Thanks in advance.

Code: Select all
        public void create()
        {
            Document doc = new Document();

            addnew(doc, this.dataGridView1, 0, 2, "title1");
            addnew(doc, this.dataGridView1, 1, 3, "title2");
            addnew(doc, this.dataGridView1, 2, 4, "title3");
            addnew(doc, this.dataGridView1, 3, 5, "title4");

            doc.SaveToFile("addtables.docx", FileFormat.Docx);
        }
        public void addnew(Document doc,DataGridView dataGrid, int startIndex, int endIndex, string title)
        {
            //add title
            Section sec = doc.AddSection();
            TextRange tr = sec.AddParagraph().AppendText(title);
            tr.ApplyCharacterFormat(new Spire.Doc.Formatting.CharacterFormat(doc) { FontSize = 24f, Position = 15f});

            Table table = sec.AddTable(true);
            string[] headers = new string[dataGrid.ColumnCount];
            for(int i = 0; i < dataGrid.ColumnCount; i++)
            {
                headers[i] = dataGrid.Columns[i].Name;
            }

            string[][] datas = new string [endIndex + 1 - startIndex][];
            int k = 0;
            for (int i = startIndex; i <= endIndex; i++)
            {
               
                string[] data = new string[dataGrid.ColumnCount];
                for(int j = 0; j < dataGrid.ColumnCount; j++)
                {
                    data[j] = dataGrid.Rows[i].Cells[j].Value.ToString();
                }
                datas[k] = data;
                k++;
            }

            //Add Cells
            table.ResetCells(datas.Length + 1, headers.Length);

            //Header Row
            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 < headers.Length; i++)
            {
                //Cell Alignment
                Paragraph p = FRow.Cells[i].AddParagraph();
                FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                //Data Format
                TextRange TR = p.AppendText(headers[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 < datas.Length; r++)
            {
                TableRow DataRow = table.Rows[r + 1];

                //Row Height
                DataRow.Height = 20;

                //C Represents Column.
                for (int c = 0; c < datas[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(datas[r][c]);
                    //Format Cells
                    p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                    TR2.CharacterFormat.FontName = "Calibri";
                    TR2.CharacterFormat.FontSize = 12;
                    TR2.CharacterFormat.TextColor = Color.Brown;
                }
            }

            sec.AddParagraph().AppendBreak(Spire.Doc.Documents.BreakType.PageBreak);
        }


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Apr 05, 2021 1:26 pm

Hi,

I'm getting an error while running your code :
System.IO.FileLoadException: Could not load file or assembly 'Spire.License, Version=1.3.7.40, Culture=neutral, PublicKeyToken=b1144360237c8b3f' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
System.IO.FileLoadException: Could not load file or assembly 'Spire.License, Version=1.3.7.40, Culture=neutral, PublicKeyToken=b1144360237c8b3f' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


I'm attaching my project for your reference.

DB_007
 
Posts: 36
Joined: Sat Sep 08, 2018 4:45 pm

Tue Apr 06, 2021 1:28 am

Hello,

Thanks for your feedback!

Kindly note that if you want to use two or more products of our products in one project, please use the Spire.Office to achieve this. Please download the latest Spire.Office V6.3.3, clean up and re-build your project and try again.

If you encounter any questions related to our products, please feel free to contact us. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Apr 08, 2021 4:00 am

Hi,

The table is getting broken to a new page after 3 rows. Why?

DB_007
 
Posts: 36
Joined: Sat Sep 08, 2018 4:45 pm

Thu Apr 08, 2021 8:27 am

Hello,

Thanks for your inquiry!

Kindly note that if you want to add more rows in one table, you just need to change the value of the "startIndex" or the "endIndex" when you use the "addnew" function I provided.

According to the project and the workbook "bill_consumables.xlsx" you sent to me last time. Such as export the data from index 2 to 8 of the DataGrid shows when you are choosing the sheet "PUTAY Biosystems" to the "doc" Document, and set the title name is “title1". You can achieve it by using "addnew(doc, this.dataGridView1, 2, 8, "title1");" in your Export2docClick function. Here I also attached my testing code.

Code: Select all
      void Export2docClick(object sender, EventArgs e)
      {
         // Modify to suit your machine:
         string fileName = @"DocXExample.docx";

         
         Document doc = new Document();

         addnew(doc, this.dataGridView1, 2, 8, "title1");

         doc.SaveToFile(fileName, Spire.Doc.FileFormat.Docx);

         MessageBox.Show("Done");
         Process.Start(fileName);
      }
        /**
         * @doc target Document
         * @datagrid  the datagrid which provide data
         * @startIndex the index number of the start row you want
         * @endIndex the index number of the end row you want
         * @title the title you want to set
         **/
        public void addnew(Document doc,DataGridView dataGrid, int startIndex, int endIndex, string title)
      {
         //add title
         Section sec = doc.AddSection();
         TextRange tr = sec.AddParagraph().AppendText(title);
         tr.ApplyCharacterFormat(new Spire.Doc.Formatting.CharacterFormat(doc) { FontSize = 24f, Position = 15f});

         Table table = sec.AddTable(true);
         string[] headers = new string[dataGrid.ColumnCount];
         for(int i = 0; i < dataGrid.ColumnCount; i++)
         {
            headers[i] = dataGrid.Columns[i].HeaderText;
         }

         string[][] datas = new string [endIndex + 1 - startIndex][];
         int k = 0;
         for (int i = startIndex; i <= endIndex; i++)
         {
            
            string[] data = new string[dataGrid.ColumnCount];
            for(int j = 0; j < dataGrid.ColumnCount; j++)
            {
               data[j] = dataGrid.Rows[i].Cells[j].Value.ToString();
            }
            datas[k] = data;
            k++;
         }

         //Add Cells
         table.ResetCells(datas.Length + 1, headers.Length);

         //Header Row
         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 < headers.Length; i++)
         {
            //Cell Alignment
            Paragraph p = FRow.Cells[i].AddParagraph();
            FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
            //Data Format
            TextRange TR = p.AppendText(headers[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 < datas.Length; r++)
         {
            TableRow DataRow = table.Rows[r + 1];

            //Row Height
            DataRow.Height = 20;

            //C Represents Column.
            for (int c = 0; c < datas[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(datas[r][c]);
               //Format Cells
               p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
               TR2.CharacterFormat.FontName = "Calibri";
               TR2.CharacterFormat.FontSize = 12;
               TR2.CharacterFormat.TextColor = Color.Brown;
            }
         }

         sec.AddParagraph().AppendBreak(Spire.Doc.Documents.BreakType.PageBreak);
      }
   


Hope you can understand. If you encounter have any questions, just feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Sun Apr 25, 2021 3:19 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc