I'm working on a Asp.net MVC project, which have a function that allow user to upload an excel file to server,server then process data from excel and save to database, It's working perffectly until to day,as the title, I met the follwing error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
So far, I tried all the bellow solutions and many many other solutions from other site: +unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'". +Unable to cast COM object (EXCEL). +Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. +Unable to cast COM object of type 'Microsoft.Office.Interop.PowerPoint.ApplicationClass'. +: 'Unable to cast COM object of type 'System.__ComObject' to interface type. And all of them are not working.
bellow is my function :
[HttpPost]
public ActionResult Index(HttpPostedFileBase avatarFile, string userId, int? delete_old_data)
{
string message = "0";
if (avatarFile != null && avatarFile.ContentLength > 0)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
try
{
if (delete_old_data == 1)
{
int result = DatabaseController.Instant().deleteAllAccountExceptAdmin();
}
string path = Path.Combine(Server.MapPath("~/Images"),
userId + Path.GetFileName(avatarFile.FileName));
KillSpecificExcelFileProcess(Path.GetFileName(avatarFile.FileName));
avatarFile.SaveAs(path);
bool thereIsError = false;
try
{
excelWorkbook = excelApp.Workbooks.Open(path);
}
catch (Exception ex)
{
thereIsError = true;
message = "1";
}
if (null != excelWorkbook)
{
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in excelWorkbook.Worksheets)
{
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
object[,] values = (object[,])range.Value2;
if (null != values)
{
for (int i = 15; i <= values.GetLength(0); i++)
{
user us = new user();
us.user_type = 1;
us.user_meeting_type = 0;
us.age = 0;
us.avatar = "adminavatar.png";
us.email = string.Empty;
us.password = "12345";
for (int j = 2; j <= values.GetLength(1); j++)
{
try
{
if (null != values[i, j])
{
string s = values[i, j].ToString();
Console.WriteLine("s = " + s + "---j = " + j);
switch (j)
{
case 2:
us.user_login_name = s;
break;
case 3:
us.office = s;
us.office_address = s;
us.full_name = s;
break;
case 4:
try
{
us.status = Convert.ToInt32(s);
}
catch
{
}
break;
case 5:
us.position = s;
break;
default:
break;
}
}
}
catch (Exception ex)
{
message = "2";
thereIsError = true;
}
}
if (null != us)
{
try
{
DatabaseController.Instant().addUser(us);
}
catch (Exception ex)
{
}
}
}
}
else
{
thereIsError = true;
}
}
try
{
excelWorkbook.Close();
excelApp.Workbooks.Close();
excelApp.Quit();
Marshal.ReleaseComObject(excelWorkbook);
Marshal.ReleaseComObject(excelApp.Workbooks);
Marshal.ReleaseComObject(excelApp);
}
catch
{
}
}
if (thereIsError)
{
}
}
catch (Exception ex)
{
if (null != excelWorkbook)
{
try
{
excelWorkbook.Close();
excelApp.Workbooks.Close();
excelApp.Quit();
Marshal.ReleaseComObject(excelWorkbook);
Marshal.ReleaseComObject(excelApp.Workbooks);
Marshal.ReleaseComObject(excelApp);
}
catch
{
}
}
message = "3";
}
}
return Redirect(Url.Action("AddUserView", "UserList", new { message }));
}
User contributions licensed under CC BY-SA 3.0