Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C#
//

 string conString = @"Data Source =.; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;";
            SqlConnection sqlCon = new SqlConnection(conString);
            sqlCon.Open();
            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();
            da.Fill(dtMainSQLData);
            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.Application.Workbooks.Add(Type.Missing);
            // 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();
                    }
                    else
                    {
                        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;
            ExcelApp.Quit();
            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\11.0.0.0__71e9bce111e9429c\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
Posted
Updated 29-Jan-14 0:56am
v6
Comments
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.

Quote:
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
 
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