473,466 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading in an Excel file.

I need to import an Excel file. For reasons I won't get into, I cannot
just link to the file or do an import specification, so I need to do it
via code.

I know of two ways of doing this - via a recordset and using the Excel
file as a datasource and also as setting up an Excel object and
stepping through that object's collections. (cells, rows, etc)

Can anyone tell me which one is better and why? They both seem to
work. Or perhaps, there is even a better way?

Long and short, we are getting a data file - in Excel - from a vendor -
and need to import it into our system. Stepping through the rows is
ideal to allow us to add business rules to the data.

Thanks
Brian

Nov 2 '06 #1
4 3119
"BerkshireGuy" <bd*****@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>I need to import an Excel file. For reasons I won't get into, I cannot
just link to the file or do an import specification, so I need to do it
via code.

I know of two ways of doing this - via a recordset and using the Excel
file as a datasource
This would require you to link to it using the linked table method so ...
and also as setting up an Excel object and
stepping through that object's collections. (cells, rows, etc)
.... this is your only option isn't it?
>
Can anyone tell me which one is better and why? They both seem to
work. Or perhaps, there is even a better way?
I've used both and there's not much to choose between them, performance
wise, as far as I can tell at any rate. I only moved to manipulating Excel
using code because I had to *write* data to Excel, and with SP2 on Office
this is no longer supported via the linked table method.

HTH - Keith.
www.keithwilby.com
Nov 2 '06 #2
Keitth - Actually I can access the Excel workbook as a recordset
without a linked table. I do so by creating and ADODB connection and
use Excel as the DSN.

I can provide air code if you'd like.

-Brian

Keith Wilby wrote:
"BerkshireGuy" <bd*****@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
I need to import an Excel file. For reasons I won't get into, I cannot
just link to the file or do an import specification, so I need to do it
via code.

I know of two ways of doing this - via a recordset and using the Excel
file as a datasource

This would require you to link to it using the linked table method so ...
and also as setting up an Excel object and
stepping through that object's collections. (cells, rows, etc)

... this is your only option isn't it?

Can anyone tell me which one is better and why? They both seem to
work. Or perhaps, there is even a better way?

I've used both and there's not much to choose between them, performance
wise, as far as I can tell at any rate. I only moved to manipulating Excel
using code because I had to *write* data to Excel, and with SP2 on Office
this is no longer supported via the linked table method.

HTH - Keith.
www.keithwilby.com
Nov 2 '06 #3
"BerkshireGuy" <bd*****@yahoo.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Keitth - Actually I can access the Excel workbook as a recordset
without a linked table. I do so by creating and ADODB connection and
use Excel as the DSN.

I can provide air code if you'd like.
Ah, ADO ... it is evil and must be stopped ;-) Just kidding. Yes I'm always
keen to learn new stuff and I've never used ADO, please do post your code.

Regards,
Keith.
Nov 2 '06 #4
Here it is:

Feel free to comment everyone. Good method?

Dim cn As New ADODB.Connection, RS As New ADODB.Recordset
Dim i As Integer
Dim strSourcePath, strSQL As String
strSourcePath = "C:\ExcelDocs\"
'---note: need semicolon at end of .xls; for ADO to Excel
strSourcePath = strSourcePath & "Book1.xls;"
RS.CursorLocation = adUseClient
'cn.Mode = adModeReadWrite
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strSourcePath & _
"Extended Properties=""Excel 8.0;HDR=NO;"""

strSQL = "SELECT * FROM [Sheet1$]"
RS.Open strSQL, cn, adOpenDynamic, adLockPessimistic
i = 3
With RS
.MoveFirst
.AbsolutePosition = i
Do While Not .EOF

'Other Code here
.MoveNext
Loop
End With
RS.Close
cn.Close

Keith Wilby wrote:
"BerkshireGuy" <bd*****@yahoo.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Keitth - Actually I can access the Excel workbook as a recordset
without a linked table. I do so by creating and ADODB connection and
use Excel as the DSN.

I can provide air code if you'd like.

Ah, ADO ... it is evil and must be stopped ;-) Just kidding. Yes I'm always
keen to learn new stuff and I've never used ADO, please do post your code.

Regards,
Keith.
Nov 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David Alliet | last post by:
Hello, I'm having a bit of a problem with ASP and Excel. A client has developed his own program, which calculates alot of financial stuff, in excel. I'm doing his websites and he has requested...
1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
1
by: Turner, GS \(Geoff\) | last post by:
> -----Original Message----- > From: siliconwafer > Posted At: 19 August 2005 15:20 > Posted To: c > Conversation: reading an excel file in C? > Subject: reading an excel file in C? > > >...
4
by: Phoebe. | last post by:
Hi, Good Day! Reading 1 excel file into a dataset is fine. How can I read multiple excel with the same data structure into 1 dataset? How can I append those data? Can someone help? Thanks in...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
3
by: Brad | last post by:
I'm having a problem reading data from an Excel file into a dataset. Can anybody give me an idea of what's happening? I've included the problematic source and the error message to the end of this...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
1
by: ndedhia1 | last post by:
I was reading in a log file like this that had no milliseconds: QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size QuoteBlockTiming...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.