473,404 Members | 2,137 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,404 software developers and data experts.

How to read data from Excel spreadsheet?

Hi All,

I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:

StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));

//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);

try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."

I checked the Sheet name, it is "Sheet1", what am I doing wrong?

Thanks for the help!

May 25 '07 #1
5 43399
On May 25, 11:05 am, barbara_d...@yahoo.com wrote:
Hi All,

I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:

StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));

//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);

try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."

I checked the Sheet name, it is "Sheet1", what am I doing wrong?

Thanks for the help!
I access Excel data from VB.NET, but I use the ODBC Microsoft Excel
Driver, DriverID 790. If that driver works similar to the driver you
are using then you need to suffix the worksheet name with the dollar
sign character, as in:

OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1$]",cnExcel);
May 25 '07 #2
On May 25, 8:31 am, z...@construction-imaging.com wrote:
On May 25, 11:05 am, barbara_d...@yahoo.com wrote:


Hi All,
I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:
StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));
//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);
try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."
I checked the Sheet name, it is "Sheet1", what am I doing wrong?
Thanks for the help!

I access Excel data from VB.NET, but I use the ODBC Microsoft Excel
Driver, DriverID 790. If that driver works similar to the driver you
are using then you need to suffix the worksheet name with the dollar
sign character, as in:

OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1$]",cnExcel);- Hide quoted text -

- Show quoted text -
Thank you for the response.

I tried the spreadsheet name with $ sign, the exception message is:
'"Sheet1$' is not a valid name. Make sure that it does not include
invalid characters or punctuation and that it is not too long."

Can I get more suggestions?

Thanks!

May 25 '07 #3
On May 25, 9:09 am, barbara_d...@yahoo.com wrote:
On May 25, 8:31 am, z...@construction-imaging.com wrote:


On May 25, 11:05 am, barbara_d...@yahoo.com wrote:
Hi All,
I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:
StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));
//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);
try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."
I checked the Sheet name, it is "Sheet1", what am I doing wrong?
Thanks for the help!
I access Excel data from VB.NET, but I use the ODBC Microsoft Excel
Driver, DriverID 790. If that driver works similar to the driver you
are using then you need to suffix the worksheet name with the dollar
sign character, as in:
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1$]",cnExcel);- Hide quoted text -
- Show quoted text -

Thank you for the response.

I tried the spreadsheet name with $ sign, the exception message is:
'"Sheet1$' is not a valid name. Make sure that it does not include
invalid characters or punctuation and that it is not too long."

Can I get more suggestions?

Thanks!- Hide quoted text -

- Show quoted text -
Barbara,

I would have thought the previous suggestion would have worked for
you. I understand the '$' notation doesn't work in some case though.

Have you seen this example:
http://support.microsoft.com/default...b;EN-US;316934

HTH
-Jay

May 25 '07 #4
On May 25, 10:08 am, Jay Riggs <jay.s.ri...@gmail.comwrote:
On May 25, 9:09 am, barbara_d...@yahoo.com wrote:


On May 25, 8:31 am, z...@construction-imaging.com wrote:
On May 25, 11:05 am, barbara_d...@yahoo.com wrote:
Hi All,
I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:
StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));
//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);
try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."
I checked the Sheet name, it is "Sheet1", what am I doing wrong?
Thanks for the help!
I access Excel data from VB.NET, but I use the ODBC Microsoft Excel
Driver, DriverID 790. If that driver works similar to the driver you
are using then you need to suffix the worksheet name with the dollar
sign character, as in:
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1$]",cnExcel);- Hide quoted text -
- Show quoted text -
Thank you for the response.
I tried the spreadsheet name with $ sign, the exception message is:
'"Sheet1$' is not a valid name. Make sure that it does not include
invalid characters or punctuation and that it is not too long."
Can I get more suggestions?
Thanks!- Hide quoted text -
- Show quoted text -

Barbara,

I would have thought the previous suggestion would have worked for
you. I understand the '$' notation doesn't work in some case though.

Have you seen this example:http://support.microsoft.com/default...b;EN-US;316934

HTH
-Jay- Hide quoted text -

- Show quoted text -
Thank you so much! I'll look at this information.

May 25 '07 #5
Hi,

Most probably the sheet name is incorrect,
Take a look at this code, it does read an excel without any problem, let me
know if it worked for you

string srcConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
sourceFile + @";Extended Properties=""Excel 8.0;HDR=YES;""";
string srcQuery = "Select * from [" + GetExcelSheetNames(
sourceFile)[0] + "]";

OleDbConnection srcConn = new OleDbConnection( srcConnString);
srcConn.Open();
OleDbCommand objCmdSelect =new OleDbCommand( srcQuery, srcConn);

readerExcel = objCmdSelect.ExecuteReader(
CommandBehavior.CloseConnection);
Here is how you get the name of the sheets
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();
}
}
}
<ba**********@yahoo.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
Hi All,

I need to read data from a Excel spreadsheet, but I got the problem
when I tried the code below:

StringBuilder sbConn = new StringBuilder();
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" );
sbConn.Append(excelFile);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));

//open spreadsheet and query data
OleDbConnection cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdExcel = new OleDbCommand("Select * From
[Sheet1]",cnExcel);

try
{
OleDbDataReader drExcel = cmdExcel.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The exception message is "The Microsoft Jet database engine could not
find the object 'Sheet1'. Make sure the object exists and that you
spell its name and the path name correctly."

I checked the Sheet name, it is "Sheet1", what am I doing wrong?

Thanks for the help!

May 25 '07 #6

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

Similar topics

6
by: Phil Powell | last post by:
What would one best recommend to parse an existing Excel spreadsheet (was done in Excel 97 or 2000 not sure to be honest)? I am looking for the most practical way of parsing an existing...
7
by: palgrave | last post by:
Hi, How do you create an html link from a web page to a read-only excel spreadsheet without the browser asking if the user wants to open it or save it?
4
by: Michael C# | last post by:
Hi all, I have a little program that uses OleDb to open and read an Excel spreadsheet from VB.NET. The problem I'm running into is it's not reading the column headers... The Excel worksheet...
3
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a...
1
by: lanem | last post by:
I am trying to read and excel spreadsheet using ado.net in asp.net 2.0(vb.net). It seems to work fine, but I cannot read some cells. There are some cells, that contain numbers, that I always just...
4
by: Rob Coventry | last post by:
Is it possible to read cells from an Excel spreadsheet, and what is the code for this? Many thanks. Rob.
2
by: =?Utf-8?B?SmVmZg==?= | last post by:
How do you read an excel spreadsheet cell on 64bit windows without using MS.JET (because it hasn't been ported) or without using Office interop (Because I'm not allowed to install it on the...
4
by: zacks | last post by:
I have an application that can read an Excel spreadsheet on a computer that doesn't have Excel installed on using the Jet ODBC 4.0 driver. I know need to modify the application to also be able to...
0
by: attraankit | last post by:
i am working in asp. my concern is how to read an excel spreadsheet using asp, especially when the range in the excel spreadsheet is unknown. i am designing a user form where the use has to browse...
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: 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
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,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.