473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Opening existing excel file in VB.net 2005

Hi there

I'm fairly new to .net and learning as I go using books and the net. I have some 4 years of VB6 behind me.

I'm writing an app in which I need to import data from a spread sheet.

I have so far wasted several hours trying to work out how to simply open the file.

In the msdn help files I found the code below, problem is it doesn't work..:
Dim wb As Excel.Workbook = _
Application.Workbooks.Open("C:\YourPath\YourWorkbo ok.xls")

I have added excel to my COM references.

I would be very grateful if someone could give me a bit of code to do this very basic thing.

Thanks very much in advance!!
Rino
Sep 7 '07 #1
5 5719
Hi Rino,

This program will not only open an excel file but it will also read the content of the file.. the sample program i created for you will read data in your excel file and insert it into a dataset.. no need to add COM or any references.


'first specify the path and filename of your excel
Dim PathFilename As String = "C:\TESTME.xls"
'second is specify the sheet name you want to read
Dim strSheetName As String = "Sheet1"

'create a connection
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DS As System.Data.DataSet
Dim _sqlqry As String
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Micros oft.Jet.OLEDB.4.0; " & _
"data source='" & PathFilename & " '; " & "Extended Properties=Excel 8.0;")
'Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & strSheetName & "$]", MyConnection)
DS = New System.Data.DataSet
MyCommand.Fill(DS)
MyConnection.Close()
MyConnection.Dispose()
MyConnection = Nothing

================================================== ===


Hi there

I'm fairly new to .net and learning as I go using books and the net. I have some 4 years of VB6 behind me.

I'm writing an app in which I need to import data from a spread sheet.

I have so far wasted several hours trying to work out how to simply open the file.

In the msdn help files I found the code below, problem is it doesn't work..:
Dim wb As Excel.Workbook = _
Application.Workbooks.Open("C:\YourPath\YourWorkbo ok.xls")

I have added excel to my COM references.

I would be very grateful if someone could give me a bit of code to do this very basic thing.

Thanks very much in advance!!
Rino
Sep 7 '07 #2
Hi aliasruel

That works an absolute treat! Thank you so much, you've made my day and saved both my head and the wall :-)

Kind regards,
Rino :-)
Sep 10 '07 #3
No problelm...

Best Regards
Ruel
[EMAIL REMOVED]




Hi aliasruel

That works an absolute treat! Thank you so much, you've made my day and saved both my head and the wall :-)

Kind regards,
Rino :-)
Sep 10 '07 #4
Hi Ruel

I have been having a few problems with the above project..

I'm importing info for tires from the spreadsheet.

The main categories and rim sizes are put in a line by themselves in large print.

To get the categories (passenger, truck, tractor etc) I have to add in a formula to make it a row in the dataset. for obvious reasons the dataset ignores the large headers (o further column info?!). Unfortunately I need them.
The same with the rim diameter.

For some other reason the 'tread' column just doesn't show so I have to copy that into another column which it then does pick up ??

I can't really expect our customers to do this manually before importing.
I could do it for them but of course I have to get into the sheet to do so..

In VB6 I used to just go into the sheet and do whatever I wanted. Not as elegant as your method but at least I could make it do what I wanted.

So all in all I would really like to be able to open and manually manipulate the sheet.
As described above I didn't have much luck with that..

I'm quite happy to attach a copy of the sheet, just not sure this second how to do so..

Can you, or anyone else, help please?

Thanks very much in advance!
Sep 16 '07 #5
hi i know code for update excel file which is given below
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim myCommand As New System.Data.OleDb.OleDbCommand
Dim sql As String

MyConnection = New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + _
"'c:\testfile.xls';Extended Properties=Excel 8.0;")

MyConnection.Open()
myCommand.Connection = MyConnection
sql = "Update [Sheet1$] set name = 'New Name' where id=1"
myCommand.CommandText = sql
myCommand.ExecuteNonQuery()
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
MsgBox("Updated ")
End Sub
End Class



now do any know if the cell S1 has no column name then how can i update it?
Jul 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Dixie | last post by:
How do I, in VBA from an access form module, add 5 rows to the top of a spreadsheet called MySpreadsheet.xls. The worksheet is called MyWorksheet and there is already data in the first 5 rows. I...
4
by: Josh Behl | last post by:
When I try to programmatically open an existing Excel document using a custom windows form, it works perfectly. I instanciate a new instance of the Excel.ApplicationClass and then set the Visible...
1
by: Boomessh | last post by:
Hi, I am trying to open an existing excel workbook. It pops up an error stating that “Errors were detected in 1598.xls but Microsoft Office Excel was able to open the file by making the repairs...
0
by: sandeep ch | last post by:
hi , I am opening a excel sheet from perl but every time i run the program i get a pop up message saying that "This Workbook contains links to other sources" " - To update all linked information...
2
by: Mad Scientist Jr | last post by:
>From an asp.net web page I want the user to open the results of a SQL query in Excel, as automatically as possible (ie not having to loop through columns, rows, in code). For this,...
5
by: aran | last post by:
Hello, I have about 200 records in an Access database that each have a corresponding Excel file attached to it. I have this code Set xlApp = New Excel.Application With xlApp ...
0
by: okalpana | last post by:
Hi friends, trying to do a application program.. what is the code to open a existing word document or excel file in Visual Basic 5 with a command button. i.e Clicking th button should open a...
0
by: shyshack | last post by:
Hi, I am quite new in C#. I am writing an application in which I have to open Excel file to import data from it. When opening small files everything works ok - the problem appears when I try to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.