Solved

export to excel using vb.net 2005

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 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