Locking excel file while writing
-
Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.
Gurpreet
-
Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.
Gurpreet
-
You might want to look at sharing and the MultiUserEditing[^] flag. Maybe this[^] can also help.
My signature "sucks" today
I need to support Office 2005 as well so MultiUserEditing flag will not help. also MultiUserEditing is readonly property so I cannot set it :( Also, protection property will not help in this case :(
Gurpreet
-
Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.
Gurpreet
If you are opening the excel file using a filestream you could use the fileshare parameter to prevent any other users from reading the file.
-
If you are opening the excel file using a filestream you could use the fileshare parameter to prevent any other users from reading the file.
No I am not using file stream. This is how I open and use _objAppln = new Excel.Application(); // To initialize excel file //_objAppln.Visible = true; if (_objAppln != null) { _objWorkBooks = _objAppln.Workbooks; _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file _objWorkSheet = (Excel.Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file .... Excel.Range objRange = _objWorkSheet.get_Range(columnName + _currentRowIndex, LastColumnName + _currentRowIndex); objRange.Value2 = GroupName; ... _objWorkBook.SaveAs(_fileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Gurpreet
-
No I am not using file stream. This is how I open and use _objAppln = new Excel.Application(); // To initialize excel file //_objAppln.Visible = true; if (_objAppln != null) { _objWorkBooks = _objAppln.Workbooks; _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file _objWorkSheet = (Excel.Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file .... Excel.Range objRange = _objWorkSheet.get_Range(columnName + _currentRowIndex, LastColumnName + _currentRowIndex); objRange.Value2 = GroupName; ... _objWorkBook.SaveAs(_fileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Gurpreet
Potentially you could change the read only attribute on the file as soon as you open it, and manage it that way (i.e. convert it back to read/write for the file save). This isn't perfect, though, because it would be in line for a race condition occurring.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Potentially you could change the read only attribute on the file as soon as you open it, and manage it that way (i.e. convert it back to read/write for the file save). This isn't perfect, though, because it would be in line for a race condition occurring.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
I need to add rows to that sheet. How can I change it to readonly?
Gurpreet
-
I need to add rows to that sheet. How can I change it to readonly?
Gurpreet
As soon as you open the file, change the readonly file attribute to true. Add your rows, and change the attribute to false - save - then reset to true.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.