T
tshad
I am trying to read some excel sheets and am getting what seems to be 2
sheets but I have only one sheet in one of my tables ( I do want to be able
to read multiple sheets).
My code is:
**********************************************************
string strSQL = "SELECT * FROM [{0}$]";
string mstrConnectionXLS = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
mstrConnectionXLS = string.Format(mstrConnectionXLS, strFile);
connection = new OleDbConnection(mstrConnectionXLS);
connection.Open();
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null);
if (dt == null)
{
return null;
}
foreach (DataRow row in dt.Rows)
{
tableName = row["TABLE_NAME"].ToString().Replace("$", "");
da = new OleDbDataAdapter(String.Format(strSQL,
Regex.Replace(tableName, "'", "")), connection);
da.Fill(ds, tableName);
}
**************************************************************
This works pretty well but in my loop I am getting 2 table names:
Query1
Query1$
My program thinks there are 2 sheets so it ends up running it with the same
TableName.
If I look at my debugger, it shows the same data for both dt.Rows[0] and
dt.Rows[1].
Why is that and how do I tell how many sheets I have?
I thought this was the way?
Thanks,
Tom
sheets but I have only one sheet in one of my tables ( I do want to be able
to read multiple sheets).
My code is:
**********************************************************
string strSQL = "SELECT * FROM [{0}$]";
string mstrConnectionXLS = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
mstrConnectionXLS = string.Format(mstrConnectionXLS, strFile);
connection = new OleDbConnection(mstrConnectionXLS);
connection.Open();
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null);
if (dt == null)
{
return null;
}
foreach (DataRow row in dt.Rows)
{
tableName = row["TABLE_NAME"].ToString().Replace("$", "");
da = new OleDbDataAdapter(String.Format(strSQL,
Regex.Replace(tableName, "'", "")), connection);
da.Fill(ds, tableName);
}
**************************************************************
This works pretty well but in my loop I am getting 2 table names:
Query1
Query1$
My program thinks there are 2 sheets so it ends up running it with the same
TableName.
If I look at my debugger, it shows the same data for both dt.Rows[0] and
dt.Rows[1].
Why is that and how do I tell how many sheets I have?
I thought this was the way?
Thanks,
Tom