Apr 14, 2011 at 7:38 PM
Edited Apr 14, 2011 at 8:33 PM
Quite a bit of additional testing completed. Unfortunately, I cannot use ProtectSheet under IIS, while it seems to work fine as a console app. I've tried with application pool id as Network Service, a specific user, and as Local System. Every other method
that I implement seems to work but ProtectSheet always fails with the above message, regardless of the user id, impersonation, pool id, etc. Does anyone have any suggestions on how to overcome this issue?
While debugging, when I call the Worksheet.ProtectSheet("Test") method, the following code is executed in HSSFSheet.cs (NPOI source code):
* Sets the protection enabled as well as the password
* @param password to set for protection. Pass <code>null</code> to remove protection
public void ProtectSheet(String password)
Sheet.ProtectSheet(password, true, true); //protect objs&scenarios(normal)
Following the debug on Sheet.ProtectSheet leads to InternalSheet.cs:
* protect a spReadsheet with a password (not encypted, just Sets protect
* flags and the password.
* @param password to Set
* @param objects are protected
* @param scenarios are protected
public void ProtectSheet(String password, bool objects, bool scenarios)
int protIdx = -1;
ProtectRecord prec = Protect;
PasswordRecord pass = Password;
prec.Protect = true;
The system throws an exception on prec.Protect, showing an "Object reference not set to an instance of an object." message, with the stacktrace similar to the previous post.
This also happens if I create a new workbook, create a new worksheet, and then attempt to do ProtectSheet on the new worksheet.
I've tried this under Windows 7 and on my production Windows Server 2003 machine. It fails in the same manner on both of them.
Is there some permission or other bit of code that I'm missing that would keep this from executing under IIS?
Edit: A little extra info... I'm using Excel xp/2003 files that have never had protection enabled. I think that's where the problem originates from, but I'm not sure how to go about forcing a protection pattern onto the file, if that's actually the issue.