473,394 Members | 1,567 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,394 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 2117
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.