Click here to Skip to main content
15,117,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m deveoping window application in c#.

my datagridview data is easily exported to excel.
but i m getting error when i also want to export datagridview's headers to excel.
error is : Exception from HRESULT: 0x800A03EC

and i m using code below
public void ExcelConversion()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            //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[]
                    if (i == 0)
                    {
                        DataGridViewCell cell1 = dataGridView1[j, i];
                        //for (int m = 0; m < dataGridView1.Columns.Count; m++)
                        //{

                            xlWorkSheet.Cells[1, j] = dataGridView1.Columns[j].HeaderText.ToString();
                        //}
                        //MessageBox.Show(dataGridView1.Columns[0].HeaderText);
                    }
                    else
                    {
                        xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                    }
                }
            }

            xlWorkBook.SaveAs("csharp.net-informations.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);


can anyone help me please?
THANKS IN ADVANCE
Posted
Updated 5-Jan-11 23:27pm
v2

1 solution

Solved here:
http://www.codeguru.com/forum/showthread.php?t=421702[^]

Regards
Espen Harlinn
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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