Solved

export to excel using vb.net 2005

Posted on 2008-06-12
4
1,892 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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