Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I want to export datagridview data to excel but after 1198 records it is showing the error Exception from HRESULT: 0x800AC472


please help me

my code here


Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();



Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


C#
// creating new Excelsheet in workbook
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

            // see the excel sheet behind the program
            app.Visible = true;

            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;

            // changing the name of active sheet
            worksheet.Name = "Exported from gridview";


            // storing header part in Excel
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString().Trim();
                }
            }


            // save the application
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            workbook.SaveAs(Application.StartupPath + "\\Reports\\" + System.DateTime.Now.ToString("dd-MMM-yy") + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Posted
Comments
IpsitaMishra 18-Nov-13 2:28am    
go through the link
http://stackoverflow.com/questions/9515380/excel-interop-com-exception-while-running-in-background

1 solution

Check the value of cells in row no. 1198. If it starts from = or + or -, this is the reason of exception.

Have a look here to find alternative ways to export datagridview data into Excel: How to export data to Excel using c#[^]

The best and the quickest way to export data from your aplication into Excel is to use ADO.NET (OLEDB).
Work with MS Excel and ADO.NET[^]
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET[^] - uses vb.net, but vb.net is similar to C#.
 
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