Solved

What's the best way to read excel files in VB.Net

Posted on 2013-01-02
4
486 Views
Last Modified: 2013-01-02
What's the best way to read an XLS/XLSX file in VB.Net.  Right now the way I'm going about it is how we've done it in the past by the following.....

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=""Excel 12.0;HDR=No;""; Data Source=" & xlsFilePath & ";"

    da = New OleDb.OleDbDataAdapter("SELECT * FROM [Page1_1$]", strConn)
    da.TableMappings.Add("Table", "Excel")
    da.Fill(ds)

Then I access a certain row/col by the following......

ds.Tables(0).Rows.Item(j).ItemArray(1)

Is this the best way to be doing this or should I be using the Microsoft.Office.Interop.Excel that I've been reading about?  I messed with it quickly and the first thing I noticed is it actually opened the XLS file in an Excel Window.  Are there reasons for using this versus what I've been using?  The only real issue I've seen with how I've been doing it is if I have a cell that has over 255 characters.  It has issues with that unless it's in the first X amount of rows.  That isn't a big deal.  Other then that we've never really had any issues.  I was just asked if this was the old way of doing it and if the Microsoft.Office.Interop.Excel was what we should be moving towards.

Any thoughts/suggestions/comments would be greatly appreciated.

Thanks.
0
Comment
Question by:pmac38CDS
4 Comments
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
Comment Utility
From what I've seen, part of the issue of using Microsoft.Office.Interop.Excel is that it requires you to have a license for Excel. I've also read that it's not a recommended solution for web servers (or perhaps it was servers in general).

The benefit of using Microsoft.Office.Interop.Excel is, I believe, you can finely control cells just like you would modify them in Excel. If you're just looking for something to quickly read the data and don't care about the quirk of it reading the first X rows to find out what type it is, I'd stick with that if for no other reason than to save yourself the headache of licensing and overhead of opening excel.
0
 
LVL 5

Expert Comment

by:mlaise
Comment Utility
I would use the OLE driver since you do not need a copy of Excel installed on the computer running the application.

The Excel object is good when you have to design an advanced spreadsheet from data.  For reading simple data it will do nothing for you but add more overhead and require Office to be installed on the client computers.
0
 
LVL 1

Author Comment

by:pmac38CDS
Comment Utility
Thanks!!
0

Featured Post

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.

Join & Write a Comment

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…
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 discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

12 Experts available now in Live!

Get 1:1 Help Now