Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
during writing values to an excel from c#, at the same time the excel file is open it shows the error that the file is in readonly , so how can we able to avoid that error during write stream from c# to the Excel

What I have tried:

 //Get all the sheets in the workbook
 mWorkSheets = mWorkBook.Worksheets;
 //Get the allready exists sheet
 mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
 Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

 int colCount = range.Columns.Count;


 int rowCount = range.Rows.Count+1;
 for (int index = 0; index < NoOfRecords; index++)
 {
     for (int j = 0; j < colCount; j++)
     {                       
         mWSheet1.Cells[(rowCount) + index, j + 1] ="'"+Convert.ToString(ResultsData.Rows[index][j].ToString());
     }
 }


 mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
 Missing.Value, Missing.Value, Missing.Value,
 Missing.Value, Missing.Value);
mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
Posted
Updated 18-Sep-16 5:38am
v2

If the file is locked open by another process there is nothing you can do about it. You have to write the content to a different file or you have to wait until the file is closed by the other process.
 
Share this answer
 
Quote:
during writing values to an excel from c#, at the same time the excel file is open it shows the error that the file is in readonly , so how can we able to avoid that error during write stream from c# to the Excel

2 or more Excels can work on same WorkBook at a same time. The WorkBook must be handled as shared by Excel.
I don't know if Interop can handle this and how.
 
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