Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

export to excel using vb.net 2005

Posted on 2008-06-12
4
Medium Priority
?
1,896 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:ljhodgett
  • 2
4 Comments
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21767921
Without excel it wont work.
And you dont have to add a reference to the excel.exe, but to the Microsoft Excel Object Library.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 21768277
You have a couple of workarounds if you don't have Excel fully installed:
1. instead of a real Excel file, create a .csv file which is only a text file in which you separate each field with a delimiter (like a coma or a tab). this format is recognized by Excel
2. Ues 3rd party products like http://www.aspose.com/categories/file-format-components/aspose.cells-for-.net-and-java/default.aspx
0
 
LVL 27

Expert Comment

by:planocz
ID: 21774043
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

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23469273.html
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21801035
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question