0

Hi have been struggling with this issue for the last 2 days, and cannot seem to figure out what the problem is, I have seen the other related posts on this but none of the are directly related the way I implemented my code, I am not using a Stream-reader/Writer I am simply copying the file to the folder specified, I tried to close the file reader after I read the file but that did not help either, I am out of ideas, can anyone please guide me on this code or perhaps suggest another efficient way to implement this. The file is by no means open in another location.

public ActionResult Upload(HttpPostedFileBase file)
{
    List<CleanSupplierClaim> supplierClaimsData = new List<CleanSupplierClaim>();

    try
    {
        if (file.ContentLength > 0)
        {
            if (file.FileName.EndsWith("xls") || file.FileName.EndsWith("xlsx")) ;

            string path = AppDomain.CurrentDomain.BaseDirectory 
                            + "upload\\" + file.FileName;

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            file.SaveAs(path);

            Excel.Application application = new Excel.Application();
            Excel.Workbook workbook = application.Workbooks.Open(path);
            Excel.Worksheet worksheet = workbook.ActiveSheet;
            Excel.Range range = worksheet.UsedRange;
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Papi
  • 555
  • 13
  • 40
  • 3
    The classes in the `Excel` namespace are IDisposable. Try putting them in `using` blocks or calling `Dispose()` explicitly. – Wai Ha Lee Feb 24 '16 at 08:09
  • 2
    On which line did you get the exception? – Steve Feb 24 '16 at 08:10
  • @Wai Ha Lee I will attempt that now, Steve it fails on this line here System.IO.File.Delete(path); – Papi Feb 24 '16 at 08:13
  • 1
    See [Closing Excel Application Process in C# after Data Access](http://stackoverflow.com/q/17777545/1364007) also - I suspect you'll need to close the Excel process(es) hanging around from not being disposed as they'll be locking the file and stopping you from deleting it. – Wai Ha Lee Feb 24 '16 at 08:14
  • Automating office products from within services has been unsupported... for at least as long as the .NET platform has existed. Also, what is that `if` code testing extensions meant to achieve? – Damien_The_Unbeliever Feb 24 '16 at 08:18
  • 1
    See [Considerations for server-side Automation of Office](https://support.microsoft.com/en-us/kb/257757): "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment." – Damien_The_Unbeliever Feb 24 '16 at 08:22
  • Damien, so what suggestions can you give, as my client's requirements it to upload an excel file using ASP.Net application. And the if is meant to check for correct file extension, its not complete yes, I need to implement an else to show a success or error in a partial view if the extension is wrong, ignore that line of code. – Papi Feb 24 '16 at 08:27
  • @Papi Uploading an Excel file does not require opening it in Excel. Also, what's the point of opening an instance of Excel *on the server*? What are you trying to achieve? If you just want to read the records from sheets and do something with them, you could use ADO.NET to access the file. – Thorsten Dittmar Feb 24 '16 at 08:33
  • The link I've provided has *lots* of recommendations for alternatives, depending on what type of processing you're trying to achieve. – Damien_The_Unbeliever Feb 24 '16 at 08:34
  • @Papi, you actually deal with the file as "Excel" file only after trying to delete it. Have this code worked at least once? I assume it works fine the very first time for each new file (file with a unique name) you try to upload, does it? – Artak Jan 31 '18 at 08:24

2 Answers2

0

You are opening an instance of Excel, but you're never closing it. This is not done automatically! You need to explicitly close the Excel instance. Otherwise the process will linger in the background and your file will be locked.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
-4

This has worked for me

MailMessage Mail= new MailMessage();
Mail.Attachments.Add(new Attachment(filePath, "application/vnd.ms-excel"));
Mail.Attachments.Dispose();
Paul Karam
  • 4,052
  • 8
  • 30
  • 53
Jjaved
  • 7
  • 4