Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have requirement to dump from SQL DB to excel file.There is only one database in SQL and query output in data grid view needs to be dumped in excel sheet.
I have written the following code and used  <pre>Microsoft.Office.Interop.Excel.Application Excel
.
After all the contents of DataGridView successfully dumped to excel sheet, I am getting the error attached in the screenshot video.Attaching the portion of code responsible for dump as follows.
I am new to C# and not able to troubleshoot the issue since long.

The code used to dump datagridview to excel is as follwos.
//Start exporting to excel file.
-------------------------------------------------------------------------------
using Microsoft.Office.Interop.Excel;
        //-------------------------
        private void button2_Click(object sender, EventArgs e)
        {
            /* creating Excel Application
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


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

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

            // 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();
                }
            }
            //----------------------------------------
            // Save the file
            workbook.SaveAs(("D:\\EXCEL\\AFAS_Report_Created_on_" + DateTime.Now.ToShortDateString() + ".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);
            MessageBox.Show("Your export in excel is complete.Please close this excel before createing a new one.");
            
            // Exit from the application
            app.Visible = true;
            app.Quit();===============================*/
            Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
            Worksheet ws = (Worksheet)Excel.ActiveSheet;
            Excel.Visible = true;
            ws.Cells[1, 1] = "Name of place";
            ws.Cells[1, 2] = "Element Name";
            ws.Cells[1, 3] = "No";
            ws.Cells[1, 4] = "Make Name";
            ws.Cells[1, 5] = "Model name";
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
             {
                 ws.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++)
                 {
                     ws.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                 }
             }
             int rowCount = ((System.Data.DataTable)this.dataGridView1.DataSource).Rows.Count;
             MessageBox.Show("You have exported " + rowCount.ToString() + " rows.Please close this excel before creating a new one.");
             Excel.Quit();
             }
            //End of dump to excel file.
        //------------------------------

-----------------------------------------------------------------------------------

It gives,
----------------------------------------------------
COMException occurred.
At first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll. Additional information: Exception from HRESULT:0x800A03EC
-------------------------------------------
Troubleshooting tips:
Check the Error Code property pf the exception to determine the HRESULT returned by the COM object.
-------------------------------------------------------------------------

Also in task manage multiple instances of EXCEL.exe is running.
Can somebody help me out to troubleshoot the issue?

What I have tried:

Tried a from different code project forum and stack overflow. There is a lot of post regarding this.But it did not work for me.I am new to C# too.
Posted
Updated 5-Mar-17 21:44pm

1 solution

Look at the error: it tells you what to do.
COMException occurred.
At first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll. Additional information: Exception from HRESULT:0x800A03EC
-------------------------------------------
Troubleshooting tips:
Check the Error Code property pf the exception to determine the HRESULT returned by the COM object.

So use the debugger, look at the exception object when it occurs, and read the Error Code property. That is the HRESULT value Excel returned, and a google for that (probably in hexadecimal) will tell you why Excel rejected it.
But a quick check on HRESULT:0x800A03EC suggests that it's an out of range Excel address problem.
 
Share this answer
 
Comments
Member 12708425 6-Mar-17 4:21am    
I have searched lot with the error code.But did not get any result.
Also I have to run this piece of code in Win 7 client system from which I will query to SQL Database running on my Win Server 2008 Server base machine.
Also I am seeing 3 excel instances are running in task manager.
EXCEL.EXE*32
EXCEL.EXE*32
EXCEL.EXE*32

How to kill that once I am finished with running the application.

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