The Microsoft Office Access database engine could not find an object

流过昼夜 提交于 2019-12-01 08:10:05

This error is raised because of you are trying to access sheet (which name is sheet1) in excel file. By default first sheet name is "sheet1" but user have either rename this name or delete this sheet.

To resolved this issue first of all you have to get all sheet name from excel file, then you have to pass this sheet name in your above code to import data.

string  filePath = "your file path";

string excelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0;Persist Security Info=False";

OleDbConnection Connection  = new OleDbConnection(excelconnectionstring); 


DataTable activityDataTable = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

if(activityDataTable != null)
{
    //validate worksheet name.
    var itemsOfWorksheet = new List<SelectListItem>();
    string worksheetName;
    for (int cnt = 0; cnt < activityDataTable.Rows.Count; cnt++)
    {
        worksheetName = activityDataTable.Rows[cnt]["TABLE_NAME"].ToString();

        if (worksheetName.Contains('\''))
        {
            worksheetName = worksheetName.Replace('\'', ' ').Trim();
        }
        if (worksheetName.Trim().EndsWith("$"))
            itemsOfWorksheet.Add(new SelectListItem { Text = worksheetName.TrimEnd('$'), Value = worksheetName });
    }
}

// itemsOfWorksheet : all worksheet name is added in this

so you can use itemsOfWorksheet[0] as sheet name in-place of "sheet1"

I had similar issue, I sorted it out by

  1. Saving the excel file from fileuploader to a temporary folder inside website folder.
  2. Using path to that file in my connection string

Rest all was same and now the error: The Microsoft Office Access database engine could not find the object 'sheet1$' was gone.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!