1

I build this code which exports datagridview rows into Excel file

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

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

for (i = 0; i <= dgvInventory.RowCount - 1; i++)
{
    for (j = 0; j <= dgvInventory.ColumnCount - 1; j++)
    {
        DataGridViewCell cell = dgvInventory[j, i];
        xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
    }
}

xlWorkBook.SaveAs(
    "D:\\exp.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);

it works good but problem is that im unable to export datagridview headertext. can anyone help me?

1
  • Can you post your code that worked to pass the datagridview header text to excel as well. I'm having the same issue and cant figure it out. Commented Sep 13, 2011 at 19:35

2 Answers 2

3

Do a loop before your main loop, something like this:

    for (int j = 0; j <= this.dataGridView1.ColumnCount - 1; j++)
    {
        string colName = dataGridView1.Columns[j].HeaderText;
    }

and set the header to excel worksheet row(0) or (1), column j to the value of colName.

Sign up to request clarification or add additional context in comments.

Comments

0
    Excel.Application xlApp ;
    Excel.Workbook xlWorkBook ;
    Excel.Worksheet xlWorkSheet ;
    object misValue = System.Reflection.Missing.Value;

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

    /*header text*/
    for (i = 0; i <= dgvInventory.Columns.Count - 1; i++)
    {
      xlWorkSheet.Cells[1, i+1] = dgvView.Columns[i].HeaderText; 
    }

    /*And the information of your data*/
    for (i = 0; i <= dgvInventory.RowCount - 1; i++)
    {
        for (j = 0; j <= dgvInventory.ColumnCount - 1; j++)
        {
            DataGridViewCell cell = dgvInventory[j, i];
            xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
        }
    }

    xlWorkBook.SaveAs(
        "D:\\exp.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);

4 Comments

Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
oww sorry i only just copy his code and then paste this loop
/*header text*/ for (i = 0; i <= dgvInventory.Columns.Count - 1; i++) { xlWorkSheet.Cells[1, i+1] = dgvView.Columns[i].HeaderText; }
this loop which contains the header text that will help him to export datagridview headertext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.