The Microsoft Office Access database engine could not find an object

后端 未结 2 1117
北海茫月
北海茫月 2021-01-14 11:45

I\'m trying to copy data from excel to sql server but facing the following error.

The Microsoft Office Access database engine could not find the objec

相关标签:
2条回答
  • 2021-01-14 11:54

    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"

    0 讨论(0)
  • 2021-01-14 11:55

    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.

    0 讨论(0)
提交回复
热议问题