Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i try use this code for datagridview data export in excel but every time i export excel only datagridview row export

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
{
    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    int i = 0;
    int j = 0;

    for (i = 0; i <= dataGridView1.RowCount - 1; i++)
    {
        for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
        {
            DataGridViewCell cell = dataGridView1[j, i];
            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
        }
    }
    
    xlWorkBook.SaveAs("Test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
}

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        MessageBox.Show("Exception Occured While releasing object" + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}
Posted
Updated 14-Jun-20 3:26am
v2
Comments
Richard MacCutchan 14-Jun-20 6:54am    
Run the code in your debugger to check that it gets the column count right.
Amar chand123 14-Jun-20 7:03am    
Column Count is right

Datagridview1.RowCount show 8
Richard MacCutchan 14-Jun-20 7:46am    
Well you need to give us some more clues. We cannot guess what happens when you run the code.
Amar chand123 14-Jun-20 8:21am    
I have a datagridview in datagridview data show from access database. And in datagridview i have 8 columns and 12 rows and if i use closedxml for export then data export correctly, but i don't want use closedxml because i dont know how i export same textbox data before export datagridview data
Richard MacCutchan 14-Jun-20 8:30am    
Very interesting, but that still does not explain what actually happens when you run the code. I have an application that does much the same: it creates a DataGridView and populates it with data from various sources. On pressing a button it then saves all the information in an Excel workbook. So your code should do the same, but i have no way of guessing why it does not.

1 solution

After all comments to the question here the code which works.

C#
// Column titles to Excel
for (int i= 0; i < dataGridView1.Columns.Count; i++)
{
   // Here i + 1 because Excel cell index starts at 1
   xlWorkSheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
}

// Data to Excel
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
   for (int j = 0; j < dataGridView1.Columns.Count; j++)
   {
      DataGridViewCell cell = dataGridView1[j, i];
      // Here i + 1 + 1. One +1 for excel index and another +1 for header row offset
      xlWorkSheet.Cells[i + 1 + 1, j + 1] = cell.Value;
   }
}


I hope it helps.
 
Share this answer
 
v3
Comments
Maciej Los 15-Jun-20 3:31am    
5ed!
[no name] 15-Jun-20 6:42am    
Thank you Maciej.

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