Solved

export to excel using vb.net 2005

Posted on 2008-06-12
4
1,890 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 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