Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I need help how I can check that Excel file is open which one I am going to browse to upload, i want particular Excel file if opened then save automatically and close that excel file because I am having an error when I am going to upload excel data in Data table.
following error is coming

Quote:
The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data


What I have tried:

I was trying to close excel file which one i am going to upload data, but i want check if open then close, this code does not work

C#
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel.Workbook workbook;
                    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;
                    workbook = Excel.Workbooks.Add(txtBrowse.Text);
                    workbook.Close(false, misValue, misValue);
Posted
Updated 6-Feb-18 22:21pm
Comments
Richard MacCutchan 7-Dec-16 6:05am    
You cannot close a file that is in use by another application.
Member 10192073 7-Dec-16 6:15am    
in my desktop it is, by window application i am browsing excel but i want close automatically by code when i am browing
Karthik_Mahalingam 7-Dec-16 22:23pm    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

Check whether it is open like
C#
try
{
   Stream s = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.None);

   s.Close();

   return true;
}
catch (Exception)
{
   return false;
}


Close Excel like as:-
C#
using Excel = Microsoft.Office.Interop.Excel;

# declare the application object
Excel.Application xl = new Excel.Application();

# open a file
Excel.Workbook wb = xl.Workbooks.Open("some_file.xlsx");

# do stuff ....

# close the file
wb.Close();

# close the application and release resources
xl.Quit();
 
Share this answer
 
Comments
Member 10192073 7-Dec-16 6:57am    
but its not closing my excel file. i want my file should be close, above check code is ok but i want close my proper excel file
Member 10192073 7-Dec-16 7:03am    
bool IsFileClose = CheckExcelOpen();
if (!IsFileClose)
{
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = xl.Workbooks.Open(txtBrowse.Text);
wb.Close();
xl.Quit();
}
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException ex)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}

//file is not locked
return false;
}
 
Share this answer
 
Comments
Richard Deeming 8-Feb-18 12:53pm    
As solution 1 said - FOURTEEN MONTHS AGO!

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