Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am wanting to export multiple dataGridViews to the same Excel sheet.
What I am doing is on button click add the dataGridVeiw to an array, then on the export button click, export all the dataGridViews in the array to Excel.

This works fine, however, it is putting all the datagridviews over eachother. Is there is a way to programatically place the grids underneath each other in the same sheet of Excel?

Here is some code so far

Add to array click
C#
DataGridView[] Data = DataGridView[0];
DataGridView[] Temp = new DataGridView[Data.Length + 1];
               System.Array.Copy(Data, 0, Temp, 0, Data.Length);
               Temp[Data.Length] = dgHIIT;
               MessageBox.Show("Item Added to Program", "Item Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
               Data = Temp;

Export Click

Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
          Microsoft.Office.Interop.Excel.Worksheet wsheet;
          Microsoft.Office.Interop.Excel.Workbook wbook;
          wapp = new Microsoft.Office.Interop.Excel.Application();
          wapp.Visible = false;
          wbook = wapp.Workbooks.Add(true);
          wsheet = (Microsoft.Office.Interop.Excel.Worksheet)wbook.ActiveSheet;
foreach(DataGridView item in Data)
           {

                   try
                  {
                       for (int i = 0; i < item.Columns.Count; i++)
                       {

                           wsheet.Cells[1, i + 1] = item.Columns[i].HeaderText;


                       }


                       for (int i = 0; i < item.Rows.Count; i++)
                       {

                           DataGridViewRow row = item.Rows[i];
                            for (int j = 0; j < row.Cells.Count; j++)
                            {
                               DataGridViewCell cell = row.Cells[j];
                            try
                                {

                                   wsheet.Cells[i+2, j + 1] = (cell.Value == null) ? "" : cell.Value.ToString();
                               }
                              catch (Exception ex)
                               {
                                   MessageBox.Show(ex.Message);
                                }
                            }
                        }
                   }

                   catch (Exception ex1)
                   {
                       MessageBox.Show(ex1.Message);
                   }

                }



            wapp.UserControl = true;
            wapp.Visible = true;
Posted
Updated 27-Sep-10 20:50pm
v2
Comments
Hiren solanki 28-Sep-10 2:50am    
added pre tag for visibility.

1 solution

you need to use or set cell range for plotting/writing the records to sheet.
 
Share this answer
 

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