Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody I hope you can help me. Im trying to Export Three DataGridViews In a Same Sheet In Excel but I can't do it.

Here is a code that I use to Export One DataGridView:

C#
private void CopyGridToClipboard(DataGridView grid)
        {
            //Exclude row headers
            grid.RowHeadersVisible = false;

            //Exclude column headers
            grid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            grid.SelectAll();
            DataObject dataObj = grid.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);

            //Set the visibility of row headers back
            grid.RowHeadersVisible = true;
        }



C#
private void Export_Click(object sender, EventArgs e)
        {
            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Open the excel application and add a workbook
            Microsoft.Office.Interop.Excel.Application application;
            Microsoft.Office.Interop.Excel.Workbook book;
            Microsoft.Office.Interop.Excel.Worksheet sheet;
            application = new Microsoft.Office.Interop.Excel.Application();
            application.Visible = true;
            book = application.Workbooks.Open("C:\\Doc1.xlsx");
            sheet = (Microsoft.Office.Interop.Excel.Worksheet)
            book.Worksheets.get_Item(1);

                   
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[9, 2];
            
            gridRange.Select();
            sheet.PasteSpecial(gridRange);
        }


What I have tried:

I don't know how make a copy/paste three datagridviews, maybe you have another option. Thanks.

[Update]
I want to copy the three datagrids in a Excel sheet.

First DataGrid in the cells [9,1]
Second DataGrid in the cells [20,1]
Third DataGrid in the cells [30,1]
Posted
Updated 15-Mar-16 10:34am
v2
Comments
girishmeena 15-Mar-16 15:17pm    
have you considered copying three grids to different sheet in excel?
Member 12006818 15-Mar-16 15:46pm    
Yes, But I need The Three In The Same Sheet.
First in the cells [9,1]
Second in the cells [20,1]
Third in the cells [30,1]
Patrice T 15-Mar-16 15:31pm    
Can you give details of where you want to copy the 3 grids
Member 12006818 15-Mar-16 15:48pm    
I want to copy the three datagrids in a Excel sheet.

First DataGrid in the cells [9,1]
Second DataGrid in the cells [20,1]
Third DataGrid in the cells [30,1]

It is what you mean?
Patrice T 15-Mar-16 16:26pm    
Yes

1 solution

Should try something like that :
C#
private void Export_Click(object sender, EventArgs e)
        {
            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Open the excel application and add a workbook
            Microsoft.Office.Interop.Excel.Application application;
            Microsoft.Office.Interop.Excel.Workbook book;
            Microsoft.Office.Interop.Excel.Worksheet sheet;
            application = new Microsoft.Office.Interop.Excel.Application();
            application.Visible = true;
            book = application.Workbooks.Open("C:\\Doc1.xlsx");
            sheet = (Microsoft.Office.Interop.Excel.Worksheet)
            book.Worksheets.get_Item(1);

                   
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[9, 1];
            
            gridRange.Select();
            sheet.PasteSpecial(gridRange);

            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[20, 1];
            gridRange.Select();
            sheet.PasteSpecial(gridRange);

            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[30, 1];
            gridRange.Select();
            sheet.PasteSpecial(gridRange);
        }

not tested :)

possible bug : the lonely ) is highly suspect.
C#
Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[20, 1];
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900