1

I am trying to select the "Report Details" worksheet from my excel file. However, I am having trouble selecting it,

The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly.

if (fileExtension == ".xls")
{
    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}

OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
cmd.CommandText = "SELECT * FROM [Report Details]"; //ERROR HERE
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();

Viewing my "Tables" in the dataset viewer, the connection string is able to access the file. The Report Details column is displayed as 'Report Details$'. I have tried entering it that way, but I am still getting an error.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1698144
  • 754
  • 4
  • 13
  • 36

2 Answers2

4

Your question title says EXCEL/C# Cant Find Worksheet

Your post says I am trying to select the "Report Details" WORKSHEET from my excel file.

The error you are getting is

The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly.

Solution

You are missing a $ sign

Try this (TRIED AND TESTED)

cmd.CommandText = "SELECT * FROM [Report Details$]";
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I believe the '$ and 's are causing the error. I have tried it that way with no luck. – user1698144 Dec 11 '13 at 02:03
  • My code is not for the table it for the worksheet. :) Like I said I have tried and tested it. I addressed your main problem. But anyways you have found what you `actually` wanted :) – Siddharth Rout Dec 11 '13 at 02:05
0

I was able to get the tabel name throuh dtExcelSheetName

cmd.CommandText = "SELECT * FROM [" + con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[1][2].ToString() + "]";

It is working now.

user1698144
  • 754
  • 4
  • 13
  • 36
  • 1
    This is not the actual reason for the error that you were getting `The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly. ` – Siddharth Rout Dec 11 '13 at 01:56