Click here to Skip to main content
15,886,724 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 :-
C#
string workbookPath = @"\\172.23.150.78\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;            
theWorkbook.RefreshAll();
FileInfo myfile = new FileInfo(@"\\172.23.150.78\DTRS\AllocationReport1.xlsx");
if (myfile.Exists)
                {
                    // Clear the content of the response  
                    HttpContext.Current.Response.ClearContent();

                    // 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)
                    HttpContext.Current.Response.WriteFile(myfile.FullName);

                    // End the response
                    HttpContext.Current.Response.End();
                }


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?"
Posted

1 solution

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

C#
FileInfo fi = new FileInfo(FolderAndFileName);
               if (fi.Exists)
               {
                   FileStream stream = null;
                   try
                   {
                       // 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