Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get values from a DB in my local system to Excel Sheet. This is my code

private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application Text_To_Excel = new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbooks Excel_File = (Microsoft.Office.Interop.Excel.Workbooks)Text_To_Excel.Workbooks; 
           
            Excel_File.OpenDatabase(@"C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\SportsStore.mdf");

            Text_To_Excel.ActiveWorkbook.SaveAs(location + "Mq.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

            Text_To_Excel.ActiveWorkbook.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
        }



When I run it, I get a COMException was unhandled error
Exception from HRESULT: 0x800A03EC
with error code
-2146827284


What am I doing wrong here and how do I resolve it ?
Posted

1 solution

 
Share this answer
 
Comments
Divakar Raj M 4-Apr-14 7:21am    
This worked. But can you explain why my original code didn't work. It was syntactically correct.

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