473,480 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access Excel Spreadsheets

I am currently using this to get data from an .xls
file:string conn =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file + ";" +
"Extended Properties=Excel 8.0;";
System.Data.OleDb.OleDbDataAdapter adapter =
new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM
[Sheet1$]",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
The problem is there has to be a sheet in that .xsl file, called
"Sheet1", otherwise the program generates an exception. Is there a
way to access sheets by index rather than name? Or maybe a better way
of getting data from .xsl files overall?

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 15 '05 #1
2 2369
I have not accessed sheets by index, I have however use this code to access
single sheet excel files, regardless of the sheet's name

public String getFirstSheet(){
OleDbConnection connection=null;
try{
connection=getConnection(); //This contains your logic to obtan a
connection to the excel file
System.Data.DataTable lookup;
connection.Open();
lookup
=connection.GetOleDbSchemaTable(System.Data.OleDb. OleDbSchemaGuid.Tables,nul
l);
connection.Close();
connection.Dispose();
System.String table;
table = (System.String)lookup.Rows[0]["TABLE_NAME"];
if(table.Length > 2 && table.StartsWith("$") && table.EndsWith("$")){
table = table.Substring(1, table.Length - 2);
}
return table;
}
catch(Exception ex){
if (connection==null){
connection.Dispose();
}
throw new ApplicationException("No se pudo abrir archivo",ex);
}
}

Lookup.Rows contains information about the sheets in the book, it is however
sorted alfabetically.

Anyways, hope this helps

JR

"Acephalus" <ko***@hotmail-dot-com.no-spam.invalid> escribió en el mensaje
news:40********@127.0.0.1...
I am currently using this to get data from an .xls
file:string conn =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file + ";" +
"Extended Properties=Excel 8.0;";
System.Data.OleDb.OleDbDataAdapter adapter =
new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM
[Sheet1$]",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
The problem is there has to be a sheet in that .xsl file, called
"Sheet1", otherwise the program generates an exception. Is there a
way to access sheets by index rather than name? Or maybe a better way
of getting data from .xsl files overall?

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

=---
Nov 15 '05 #2
Thanks a lot! That .GetOleDbSchemaTable() method gives me all the
sheets in the file so I can ask the user to select one. Thanks again
:D

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 15 '05 #3

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

Similar topics

18
7299
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
2
2296
by: Jim S | last post by:
To my surprise and chagrin, newer versions of Access have disabled the functionality that lets users change the data in linked tables that point to a range in an Excel workbook. This is "because of...
15
2391
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
37
5177
by: jasmith | last post by:
How will Access fair in a year? Two years? .... The new version of Access seems to service non programmers as a wizard interface to quickly create databases via a fancy wizard. Furthermore, why...
1
4605
by: Mitch | last post by:
I am using Access to create an Excel spreadsheets with graphs related to rows on the sheet1 to the graph on sheet2. I am using the same data but different subsets of the data to make different...
0
767
by: DrewYK | last post by:
I have the project that may never end in front of me. I am creating a routine that will take SpreadSheets from Excel and bring them into Access. I am not using any "DoCmd"s because the goal...
3
3589
by: MarcGA | last post by:
(Excel 2003, Access 2003, XP, novice user here) I can't get Access to accept multiple Excel files to the same table. I can import the spreadsheets to a new table, but I need to import 23...
1
5132
by: thadson | last post by:
Hi, I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory. I'm very new to this and I'm having trouble...
0
6202
by: Tony Hine | last post by:
Problem for Excel Developers One of the problems facing Excel developers moving into MS Access is actually the apparent similarity between MS Access tables and Excel spreadsheets. MS Access is...
1
1684
by: bardwell | last post by:
We have created 4 complex Excel spreadsheets with computations (no data) that we don't want to reprogram in Access. I am hoping we can open the Excel spreadsheets, and use them in the Access program...
0
7048
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
6966
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
5344
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
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
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 ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.