Solved

export to excel using vb.net 2005

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now