我的代码生成一个 OpenXML 电子表格,我想防止修改和删除操作。我的解决方案似乎有效,但我发现在 Excel 中打开它,在“审阅”面板中,我可以按下“取消保护”按钮,并且 Excel 无需询问任何密码即可取消保护选定的工作表。我怎样才能避免这种情况?
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();
}
}
}