Click here to Skip to main content
14,979,231 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:

 string conString = @"Data Source =.; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;";
            SqlConnection sqlCon = new SqlConnection(conString);
            SqlDataAdapter da = new SqlDataAdapter("select RollNo,Color_Name,Cand_Name,BatchNo from Batch_View  where BatchNo='" + BatchNo + "'", sqlCon);
            System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
            DataColumnCollection dcCollection = dtMainSQLData.Columns;  

 Export Data into EXCEL Sheet

      Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();   // this is where the problem is 
            // ExcelApp.Cells.CopyFromRecordset(objRS);
            for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)
                for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
                    if (i == 1)
                        ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
                        ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();

            ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Documents and Settings\\All Users\\Desktop\\test.xls");
            ExcelApp.ActiveWorkbook.Saved = true;
            MessageBox.Show("Data Exported Successfully into Excel File");

And got this error..

Error Message is Error 86 Cannot embed interop type 'Microsoft.Office.Interop.Excel.AppEvents_Event' found in both assembly 'c:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\\Microsoft.Office.Interop.Excel.dll' and 'c:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll'. Consider setting the 'Embed Interop Types' property to false. PET
Updated 29-Jan-14 0:56am
sameer549 29-Jan-14 7:08am
Hi khan, check if u have added 2 references of excel.or else Remove the reference and add once again. And to make 'embed interop types' to false,open solution explorer right click on reference, goto properties, and set Embed Interop Types to false.

hope it helps
Regards Sameer

1 solution

On above error it's giving the solution also.

Consider setting the 'Embed Interop Types' property to false.

So Just open the Properties tab for the assembly in Visual Studio 2010/12/13 and set "Embed Interop Types" to "False".

Please check this link if you need more info : Interop type cannot be embedded

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