Solved

How do I open and read an Excel file in Visual Basic 2010?

Posted on 2010-08-26
9
1,782 Views
Last Modified: 2013-11-27
I am trying to convert a VB 6.0 application to VB 2010.  I am having trouble with opening and reading/writing to Excel workbooks.  Can I see an example?

        Dim xlApp As New Microsoft.Office.Interop.Excel.Application
        Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
(NOW WHAT???)
0
Comment
Question by:motokent
9 Comments
 
LVL 4

Expert Comment

by:Bordeaux0113
ID: 33530575
"Excel: Demonstrates how to use Visual Basic to start Excel, create a workbook from a template, update cells in a worksheet that then populate graphs in the workbook, and perform other tasks"

This is one of many Visual Basic Samples included at this address (in a downloadable file):
http://code.msdn.microsoft.com/vb2010samples/Release/ProjectReleases.aspx?ReleaseId=4174
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33530579
I strongly recommend not using Automation to process Excel files. Better use something like http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx
or check http://support.microsoft.com/kb/302094
0
 
LVL 20

Expert Comment

by:woolnoir
ID: 33530581
what do you want to do with the excel document, you cant just ask us 'now what' as with any programming language we need an idea about what your trying to achieve to assist.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33530649
You can also read the data into a dataset, like you would do with a sqlserver, oracle, ... database

Example can be found here:
Reading Excel 2003 and 2007 Files Using OLEDB
http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:motokent
ID: 33530681
Let me give some more details. My first task is to:
1.) Open C:\test.xls
2.) Read columns A-D (all populated rows) into an arraylist in VB2010
For example:
ArrayListTest(0) would get row1:columnsA,B,C,D
ArrayListTest(1) would get row2:columnsA,B,C,D
etc.
3.) Close C:\test.xls
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33530712
If you use my example above, you'll have a datatable, which you can use to loop over the rows. You can easily query the excel to say you only want the first 4 columns.
0
 

Author Comment

by:motokent
ID: 33530737
Dhaest:  I need to use Visual Basic 2010.  Isn't the example in c#?
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33530765
Here is another example, surely written in vb.net (the version of .net has no impact)

Read Data from Excel using OLEDB
http://vb.net-informations.com/excel-2007/vb.net_excel_oledb.htm
0
 

Author Closing Comment

by:motokent
ID: 33531442
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now