Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

export to excel using vb.net 2005

Hi,

I have attached the code in question. I'm trying to export the dataset into excel. I have used this code in the past and it seems to work well. The trouble I'm having is that the pc I am currently using to develop on does not have excel installed. I have the excel.exe file and have tried adding it to the references of the project but it says its not a valid coms object. How can I get this to work please?

Many Thanks
Lee
Dim xlApp As New Excel.Application
        Dim xlWB As Excel.Workbook = xlApp.Workbooks.Add
        Dim xlWS As Excel.Worksheet = xlWB.Worksheets.Add
        xlWS.Name = dt.TableName
        'add the headers
        For c As Integer = 0 To dt.Columns.Count - 1
            xlWS.Cells(1, c + 1).Value = dt.Columns(c).ColumnName
        Next
        'add the values
        For r As Integer = 0 To dt.Rows.Count - 1
            For c As Integer = 0 To dt.Columns.Count - 1
                xlWS.Cells(r + 2, c + 1).Value = dt.Rows(r)(c)
            Next
        Next
        xlWB.SaveAs(FilePath)
        xlWB.Close()
        xlApp.Quit()
        MsgBox("Export Complete", MsgBoxStyle.Information, "Complete")

Open in new window

Avatar of rachitkohli
rachitkohli
Flag of India image

Without excel it wont work.
And you dont have to add a reference to the excel.exe, but to the Microsoft Excel Object Library.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi check this post I made today.
The last  code snippet is the best way to read to a excel file. Fast too!
You do need to file to run in excel..... excel.exe or something like.... XL5EN32.OLB

https://www.experts-exchange.com/questions/23469273/VB-NET-2005-Excel-spreadsheet-save-error.html