473,395 Members | 1,341 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,395 software developers and data experts.

Refer to sheet2 - even when named

I am using OLEDB in vb.net, and processing Excel files where I need to
refer to a specific sheet number. I don't know the name of the sheet. I
have tried:
SELECT * FROM [Sheet2$]
SELECT * FROM [Sheet2]
....

Sheet2 has a 'name' - but I don't know it. I am using a standard
connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended
Properties=""Excel 8.0;HDR=Yes""

I can easily read from Sheet1 with:
SELECT * FROM [Sheet1$]
because it is actually named Sheet1. If I rename Sheet2, or open it and
find it's name, it is easy - but I need to find a way that will handle the
thousands of files (and tons more coming). Does anyone have any tips? I
was hoping to avoid the expensive COM plugins etc.

btw - I found this to be very helpful -
http://support.microsoft.com/kb/316934/en-us
but it didn't give me my answer. If anything, it makes me think that it
isn't possible unless I know the exact cell range (which I don't)

Thanks!
Jun 13 '06 #1
3 1327
This may be helpful to you. Query the spreadsheet and return the name
property of the indexed sheet you would like to use.

http://weblogs.asp.net/donxml/archiv.../21/24908.aspx

Masa Ito wrote:
I am using OLEDB in vb.net, and processing Excel files where I need to
refer to a specific sheet number. I don't know the name of the sheet. I
have tried:
SELECT * FROM [Sheet2$]
SELECT * FROM [Sheet2]
...

Sheet2 has a 'name' - but I don't know it. I am using a standard
connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended
Properties=""Excel 8.0;HDR=Yes""

I can easily read from Sheet1 with:
SELECT * FROM [Sheet1$]
because it is actually named Sheet1. If I rename Sheet2, or open it and
find it's name, it is easy - but I need to find a way that will handle the
thousands of files (and tons more coming). Does anyone have any tips? I
was hoping to avoid the expensive COM plugins etc.

btw - I found this to be very helpful -
http://support.microsoft.com/kb/316934/en-us
but it didn't give me my answer. If anything, it makes me think that it
isn't possible unless I know the exact cell range (which I don't)

Thanks!


Jun 13 '06 #2
"Charlie Brown" <cb****@duclaw.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
This may be helpful to you. Query the spreadsheet and return the name
property of the indexed sheet you would like to use.
http://weblogs.asp.net/donxml/archiv.../21/24908.aspx


Thank you - this worked perfectly.
fwiw I ended up creating the following function:

Public Shared Function GetExcelSheetNames(ByVal fiExcel As FileInfo) As
ArrayList
Dim arl As New ArrayList
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & fiExcel.FullName & _
";Extended Properties=""Excel 8.0;HDR=Yes"""
Dim cn As New OleDbConnection(sConn)
Dim dt As New DataTable
cn.Open()
dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object()
{Nothing, Nothing, Nothing, "Table"})
cn.Close()

For Each r As DataRow In dt.Rows
If dt.Columns.Contains("TABLE_NAME") Then
arl.Add(r("TABLE_NAME"))
End If
Next

Return arl
End Function
For anyone googling and finding this, note that the sheet names end with
$, seems the last 'TABLE' is the file name. ie: excel file named MyFile
with two sheets named: Sheet1, MySheet will return 3 strings in this
arraylist - Sheet1$, MySheet$, MyFile

Jun 13 '06 #3
Masa, could you repost paying attention to line break. TIA

Greg
Masa Ito wrote:
"Charlie Brown" <cb****@duclaw.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
This may be helpful to you. Query the spreadsheet and return the name
property of the indexed sheet you would like to use.
http://weblogs.asp.net/donxml/archiv.../21/24908.aspx


Thank you - this worked perfectly.
fwiw I ended up creating the following function:

Public Shared Function GetExcelSheetNames(ByVal fiExcel As FileInfo) As
ArrayList
Dim arl As New ArrayList
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & fiExcel.FullName & _
";Extended Properties=""Excel 8.0;HDR=Yes"""
Dim cn As New OleDbConnection(sConn)
Dim dt As New DataTable
cn.Open()
dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object()
{Nothing, Nothing, Nothing, "Table"})
cn.Close()

For Each r As DataRow In dt.Rows
If dt.Columns.Contains("TABLE_NAME") Then
arl.Add(r("TABLE_NAME"))
End If
Next

Return arl
End Function
For anyone googling and finding this, note that the sheet names end with
$, seems the last 'TABLE' is the file name. ie: excel file named MyFile
with two sheets named: Sheet1, MySheet will return 3 strings in this
arraylist - Sheet1$, MySheet$, MyFile


Jun 13 '06 #4

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

Similar topics

7
by: Ivo | last post by:
How do I make the magic 'this' variable refer to an object of my choice, in a string of code which is to be eval'ed? Say, I have an object, an array with four elements: var myobject = ; and a...
4
by: MLH | last post by:
I have 9 textboxes named a1 b1 c1 d1 e1 f1 g1 h1 i1 on a form that I fill with values. I want to put those values in an array Plane1(3,3).
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
1
by: Paul | last post by:
I have a MDI container form "frmParent". On that form there is a tabstrip control with 2 pages. On one of those pages there is a textbox and a button. When the user clicks on the button a...
7
by: Ryan | last post by:
I'm wanting to find the value of TableID on the current record displayed on my form. I cannot seem to find any method within the TableAdapter class to find this. An example I've found MS uses a...
1
by: SkyTV | last post by:
i have got 15 combo Boxs on the form they are named cbo1 - cbo15 the following works just fun strY= cbo1.Text; but i wanna learn the for loop way to do it.... i have tried many...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
1
by: laredotornado | last post by:
Hi, I have this master frameset page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <TITLE>Account Creation</TITLE> <META HTTP-EQUIV="Content-Type"...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.