472,953 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problem about getting a excel datasheet's name with oledb

Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the excel
file?

Thanks in advance for any hints and reply.
Jun 27 '08 #1
6 2089
The Excel application object contains a sheets collection that you can
iterate to examine sheet.name. Google.com/codesearch should provide
examples.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------
"Kun Niu" <Ku****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the
excel
file?

Thanks in advance for any hints and reply.
Jun 27 '08 #2
On May 5, 12:06*pm, Kun Niu <Kun...@discussions.microsoft.comwrote:
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the excel
file?

Thanks in advance for any hints and reply.
Try this:
static String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
// Connection String. Change the excel file to the file you
// will search.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection
string.
objConn = new OleDbConnection(connString);
// Open connection with the database.
objConn.Open();
// Get the data table containg the schema guid.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , null);
if(dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach(DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch(Exception ex)
{
return null;
}
finally
{
// Clean up.
if(objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if(dt != null)
{
dt.Dispose();
}
}

Jun 27 '08 #3
Thanks.
But I'd rather prefer the oleconnection way.:)

"Alvin Bruney [ASP.NET MVP]" wrote:
The Excel application object contains a sheets collection that you can
iterate to examine sheet.name. Google.com/codesearch should provide
examples.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------
"Kun Niu" <Ku****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the
excel
file?

Thanks in advance for any hints and reply.
Jun 27 '08 #4
Excellent work.
This is what I'm after.
Thanks.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
On May 5, 12:06 pm, Kun Niu <Kun...@discussions.microsoft.comwrote:
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the excel
file?

Thanks in advance for any hints and reply.

Try this:
static String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
// Connection String. Change the excel file to the file you
// will search.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection
string.
objConn = new OleDbConnection(connString);
// Open connection with the database.
objConn.Open();
// Get the data table containg the schema guid.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , null);
if(dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach(DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch(Exception ex)
{
return null;
}
finally
{
// Clean up.
if(objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if(dt != null)
{
dt.Dispose();
}
}

Jun 27 '08 #5
On May 5, 6:06 pm, Kun Niu <Kun...@discussions.microsoft.comwrote:
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the excel
file?

Thanks in advance for any hints and reply.
Hi,

You can use Excel Automation or some third party component -- it works
much better. I can recommend you to use GemBox.Spreadsheet .NET
component.

--
Mario
GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX
or CSV) files or export to HTML files from your .NET apps. See
http://www.gemboxsoftware.com/GBSpreadsheet.htm
Jun 27 '08 #6
Thanks.
I'll pay attention to that.:)

"ma*********@gmail.com" wrote:
On May 5, 6:06 pm, Kun Niu <Kun...@discussions.microsoft.comwrote:
Dear all,

I'm trying to use oledb to get the content of an excel file.
Now I can use oledb to get the content of a datasheet.
But I should know the name of the datasheet in advance.
How can I use oledb to query the names of all the spreadsheets in the excel
file?

Thanks in advance for any hints and reply.

Hi,

You can use Excel Automation or some third party component -- it works
much better. I can recommend you to use GemBox.Spreadsheet .NET
component.

--
Mario
GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX
or CSV) files or export to HTML files from your .NET apps. See
http://www.gemboxsoftware.com/GBSpreadsheet.htm
Jun 27 '08 #7

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

Similar topics

4
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
2
by: jeffgeorge | last post by:
I'm currently exporting a form to Excel. Because there are controls and totals in the header, I first have a button for users to convert to a datasheet. Then I use the automated quick office...
0
by: ImraneA | last post by:
Hi there Have a database, where front-end interface allows user to select a ms access database. From there, standard tables are linked. Routine, that creates a spreadsheet, for each table a...
22
by: PeteCresswell | last post by:
I've been to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mschrt/html/vbobjtitleobject.asp, but still don't have a clue. For example, I've got a chart object namde...
3
by: hkappleorange | last post by:
I connect to mdb file using the following codes. How should I modify it if I want to connect to Excel instead ? <%@ Import Namespace="System.Data.OleDb" %> <% Dim conAuthors As...
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...
10
by: Ben | last post by:
Hi, i have a weird problem and i don't know who is responsible for this: IIS, excel or asp.net. My problem: we use an asp.net 2.0 application under IIS 6.0 (server 2003 sp2) which must write...
1
by: Cupric | last post by:
I have a python script that runs fine from the command line or from within IDLE, but doesn't work through the Vista Task Scheduler. The script downloads some csv files and then uses pywin32 to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.