Murray Brown
asked on
VB.net Interop Type "ApplicationClass" cannot be embedded. Use the application interface insteads
Hi
I am getting the following error in the code below on the line marked XXXX
Interop Type "ApplicationClass" cannot be embedded. Use the application interface insteads
Sub SQL_to_Excel()
' Create sql connection string
'Dim conString As String = "Data Source = XXXX ; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;"
Dim conString As String = "Server=xx.xx.xx.xx;Databa se=xxx;Uid =xxxUser;P wd=xxx"
Dim sqlCon As New SqlConnection(conString)
sqlCon.Open()
Dim da As New SqlDataAdapter("select * from tblTest", sqlCon)
Dim dtMainSQLData As New System.Data.DataTable()
da.Fill(dtMainSQLData)
Dim dcCollection As DataColumnCollection = dtMainSQLData.Columns
' Export Data into EXCEL Sheet
Dim ExcelApp As New Microsoft.Office.Interop.E xcel.Appli cationClas s() 'XXXX
ExcelApp.Application.Workb ooks.Add(T ype.Missin g)
' ExcelApp.Cells.CopyFromRec ordset(obj RS);
For i As Integer = 1 To dtMainSQLData.Rows.Count
For j As Integer = 1 To dtMainSQLData.Columns.Coun t
If i = 1 Then
ExcelApp.Cells(i, j) = dcCollection(j - 1).ToString()
Else
ExcelApp.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()
End If
Next
Next
ExcelApp.ActiveWorkbook.Sa veCopyAs(" C:\Users\S achindra\D esktop\tes t.xls")
ExcelApp.ActiveWorkbook.Sa ved = True
ExcelApp.Quit()
MsgBox("Data Exported Successfully into Excel File")
End Sub
I am getting the following error in the code below on the line marked XXXX
Interop Type "ApplicationClass" cannot be embedded. Use the application interface insteads
Sub SQL_to_Excel()
' Create sql connection string
'Dim conString As String = "Data Source = XXXX ; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;"
Dim conString As String = "Server=xx.xx.xx.xx;Databa
Dim sqlCon As New SqlConnection(conString)
sqlCon.Open()
Dim da As New SqlDataAdapter("select * from tblTest", sqlCon)
Dim dtMainSQLData As New System.Data.DataTable()
da.Fill(dtMainSQLData)
Dim dcCollection As DataColumnCollection = dtMainSQLData.Columns
' Export Data into EXCEL Sheet
Dim ExcelApp As New Microsoft.Office.Interop.E
ExcelApp.Application.Workb
' ExcelApp.Cells.CopyFromRec
For i As Integer = 1 To dtMainSQLData.Rows.Count
For j As Integer = 1 To dtMainSQLData.Columns.Coun
If i = 1 Then
ExcelApp.Cells(i, j) = dcCollection(j - 1).ToString()
Else
ExcelApp.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()
End If
Next
Next
ExcelApp.ActiveWorkbook.Sa
ExcelApp.ActiveWorkbook.Sa
ExcelApp.Quit()
MsgBox("Data Exported Successfully into Excel File")
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much. I need to blank out the login details
ASKER
Thanks very much
Glad I could help
Microsoft.Office.Interop.E