Click here to Skip to main content
15,846,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Excel file on a file share path and the excel sheet is connected to a SQL Table.I am trying to refresh that excel sheet programatically and then trying to download it.While downloading i am getting an exception that "The file is being used by another process".
The Code is like that :-
string workbookPath = @"\\\DTRS\AllocationReport1.xlsx";
Application excelFile = new Application();               
Workbook theWorkbook = excelFile.Workbooks._Open(workbookPath, 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);    
Sheets sheets = (Sheets)theWorkbook.Worksheets;            
FileInfo myfile = new FileInfo(@"\\\DTRS\AllocationReport1.xlsx");
if (myfile.Exists)
                    // Clear the content of the response  

                    // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

                    // Add the file size into the response header
                    HttpContext.Current.Response.AddHeader("Content-Length", myfile.Length.ToString());

                    // Set the ContentType
                    //HttpContext.Current.Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

                    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)

                    // End the response

Please give me suggestion that what i will do as i need both process compulsorly.
Firstly refreshing the Excel and Secondly Downloading the Excel.
The exception is occuring because the Excel is internally refreshing the connection as when i am debugging it i m getting an pop up message that "AllocationReport1.xlsx is being used by NETWORK SERVICE.Open read only?"

1 solution

with the next code you can see if you can exclusively open the the file:

FileInfo fi = new FileInfo(FolderAndFileName);
               if (fi.Exists)
                   FileStream stream = null;
                       // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                       using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                           if (stream.CanWrite)

If the stream.CanWrite you can access the file.

Other probles is that you check in the code if the file exists. If it does not you will not write any file!
Second if the file is found delete it first. You can always write a new file than with the same name.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900