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;
}