0

In my web application, I have added an excel upload section.

Here When the user selects the excel file and clicked the upload button the below code is running.

If there was an error in upload it returns to the main page again and, when the user tried to upload the excel file again, it returns an error.

So I checked this by opening the task manager, and then I saw that the EXCEL process is running in the background. When I end the process of the excel file from there, then I can upload the excel file again.

So is there any way if the excel process is running kill that process and run the code without any error from the excel process. ?

This is the existing code.

if (excelFile.FileName.EndsWith("xls") || excelFile.FileName.EndsWith("xlsx")) 
{
  string path = Server.MapPath("~/ExcelFile/" + excelFile.FileName);
  KillSpecificExcelFileProcess(excelFile.FileName);
  if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
  excelFile.SaveAs(path);
  ExcelPath = Server.MapPath("~/ExcelFile/") + path;
  Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
  Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(path);
  Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
  Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;
}

Dev Beginner
  • 589
  • 1
  • 11
  • Your comment… _”So is there any way if the excel process is running kill that process”_ … Killing a process that your code did not necessarily start is not necessarily the best of ideas. If your code “starts” the process then fails/crashes… then you should wrap that code in a `try/catch/finally` statement and properly dispose of the objects your code creates in the `finally` portion of the `try/catch/finally` statement. – JohnG Jan 22 '22 at 04:08
  • 1
    https://stackoverflow.com/a/25135685/8967612 – 41686d6564 stands w. Palestine Jan 22 '22 at 04:11

1 Answers1

0

Killing a process is like using a sledgehammer to drive in a panel pin. It'll work but what damage will it do in the process? when using iterop you need to use Try-Catch-Finally

try
{
    app = new Excel.Application();
    books = app.Workbooks;
    book = books.Add();
    sheets = book.Sheets;
    sheet = sheets.Add();
    range = sheet.Range["A1"];
    range.Value = "Lorem Ipsum";
    book.SaveAs(@"C:\Temp\ExcelBook" + 
   DateTime.Now.Millisecond + ".xlsx");
    book.Close();
    app.Quit();
}
finally
{
    if (range != null) Marshal.ReleaseComObject(range);
    if (sheet != null) Marshal.ReleaseComObject(sheet);
    if (sheets != null) Marshal.ReleaseComObject(sheets);
    if (book != null) Marshal.ReleaseComObject(book);
    if (books != null) Marshal.ReleaseComObject(books);
    if (app != null) Marshal.ReleaseComObject(app);
}

This will make sure that the instance of excel that you created in code is disposed of should there be an error nd will be correctly closed when you are done with it.

  • @JohnG you are 100% correct. My apologies. Its been such a long time since I used iterop. I've updated my answer. By-the-way have you thought of using Openxml? It makes working with office files so much easier and streamlined. I use it now that's why I haven't used interop in such a long time –  Jan 23 '22 at 08:12
  • If I have a choice I tend towards EPPlus... However, just about anything other than `interop` is a better choice. I am just saying that sometimes the OP may not have a choice and I tend to (possibly wrongly) accept that as a fact. But I agree, if you have a choice, there are better options for editing Excel files than `interop` – JohnG Jan 23 '22 at 09:42
  • I use ClosedXML for Excel in OpenXML. I've never tried EPPlus I must give it a go. Is it much better than ClosedXML? –  Jan 23 '22 at 20:10
  • _”Is it much better than ClosedXML?”_ … that would fall into the opinion category that only you can answer. – JohnG Jan 23 '22 at 20:42