Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default 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!
  #2   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Refer to sheet2 - even when named

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!


  #3   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Refer to sheet2 - even when named

"Charlie Brown" wrote in
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

  #4   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Refer to sheet2 - even when named

Masa, could you repost paying attention to line break. TIA

Greg
Masa Ito wrote:
"Charlie Brown" wrote in
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple? View named range from sheet1 on sheet2 susiew32 Excel Worksheet Functions 1 August 27th 09 03:29 PM
Refer to a named range indirectly tberkom Excel Worksheet Functions 1 May 13th 09 12:33 AM
???Refer to a specifc cell in a named range Jaylin Excel Worksheet Functions 1 February 10th 06 11:47 AM
How does one refer to the n-1 element of a named range? Charles Hewitt Excel Discussion (Misc queries) 2 November 26th 05 06:56 AM
How do you refer to a dynamic named range? Ian Staines Excel Programming 3 September 14th 03 10:48 PM


All times are GMT +1. The time now is 09:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"