My code generates an OpenXML spreadsheet, I want to protect against modifying and deleting operations. My solution seems working, but I found that opening it in the Excel, in the "Review" panel I can push the "Unprotect" button, and Excel unprotects the selected sheet without asking any password. How can I avoid this?
String password = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 16);
using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Open(input, true))
{
foreach (WorksheetPart worksheetPart in excelDoc.WorkbookPart.WorksheetParts)
{
SheetProtection sp = new SheetProtection()
{
Sheet = true,
SelectLockedCells = false,
SelectUnlockedCells = false,
Password = password,
Objects = true,
Scenarios = true,
// AlgorithmName = "SHA-256"
};
foreach (OpenXmlElement currentChildElement in worksheetPart.Worksheet.ChildElements)
{
if (currentChildElement is SheetData)
{
worksheetPart.Worksheet.InsertAfter(sp, currentChildElement);
worksheetPart.Worksheet.Save();
}
}
}