At the moment, I am creating a new Excel document with Open XML's SpreadsheetDocument
class by passing a MemoryStream
parameter. I now need to set a password on this SpreadsheetDocument
object, but what I have attempted does not seem to work. The Excel document open's up without asking for a password.
Below is what I have tried so far (mem
being the MemoryStream
parameter):
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mem, true))
{
foreach (var sheet in spreadsheet.WorkbookPart.WorksheetParts)
{
sheet.Worksheet.Append(new SheetProtection() { Password = "test" });
}
}
I have also attempted the following with no success:
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mem, true))
{
spreadsheet.WorkbookPart.Workbook.WorkbookProtection = new WorkbookProtection
{
LockStructure = true,
LockWindows = true,
WorkbookPassword = "test"
}
}
What am I missing please?