0

Through an C#/ASP.NET website, I'm using SpreadsheetGear to open a file from a template, then making modifications to it based off user input and then saving it a new location. When I attempt to manually open the newly created file after saving, I get a message stating that SpreadsheetGear has the file locked for editing.

Here's code snippets below:

SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(pathToTemplate);
workbook.WorkbookSet.GetLock();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["sheetName"];
SpreadsheetGear.IRange cells = worksheet.Cells;

//fill in worksheet
...

workbook.SaveAs(pathToGeneratedFiles + exportFileName, SpreadsheetGear.FileFormat.XLS97);
workbook.WorkbookSet.ReleaseLock();

worksheet = null;
workbook.Close();
workbook = null;

The only thing I can think of in the undisplayed "fill in worksheet" section that is even somewhat tricky is deleting a column and shifting the other columns to the left.

Any thoughts? Thanks.

Paul Smith Jr
  • 89
  • 5
  • 14
  • An error of "SpreadsheetGear has the file locked for editing" sounds more like something Microsoft Excel might do. SpreadsheetGear doesn't lock files while opened and editing like Excel does--it only "locks" a file for the usually brief time it takes to read/write the file from disk. What is the exact wording of the error you are receiving? – Tim Andersen Mar 27 '12 at 17:23
  • @Tim, I get a message reading: " is locked for editing by 'SpreadsheetGear for .NET 1.6.0.122'" and I'm then given the standard read-only, notify and cancel options. It looks just like the error I'd get if someone else had the file open. – Paul Smith Jr Mar 29 '12 at 19:52

2 Answers2

1

Have you tried this without using GetLock()? GetLock() is not usually used when doing asp.net programming. According to the website docs,

There is no need to use GetLock and ReleaseLock when using a workbook set which is not attached to any Windows Forms components from SpreadsheetGear.

Daniel
  • 5,602
  • 4
  • 33
  • 36
  • 1
    Daniel's correct. You do not need to use GetLock/ReleaseLock in a typical ASP.NET scenario. Also, even if you do have a valid reason to use locks, the workbook.Close() line should be executed before calling ReleaseLock(). – Tim Andersen Mar 27 '12 at 17:50
  • The original code didn't have GetLock() or ReleaseLock() and had the problem with the saved file being locked for editing. The calls to those functions were added in an attempt to resolve the situation. – Paul Smith Jr Mar 28 '12 at 14:03
  • One thing you can check is to make sure the Excel template file is closed and does not have a locking file connected to it when you are doing GetWorkbook(). If the template file has a lock on it, it could cause these problems. – Daniel Mar 28 '12 at 14:25
0

So, it turned out to an old copy of the SpreadsheetGear DLL in my project's bin folder. Once we updated it, the problem went away. Thanks for the help.

Paul Smith Jr
  • 89
  • 5
  • 14