Using ClosedXML (v0.93.1) on .Net Core 2.0, attempting to save a workbook so that when opened it prompts the user to open as "Read Only" or enter the protected password. (See linked question below).
I have seen IXLWorksheet.Protect()
and XLWorkbook.Protect()
- these correctly protect the workbook but still default the user to open with R/W access. This is a problem because it write locks the file and my process can't overwrite it until the user closes excel.
This question indicates OpenXML (the underlying API for ClosedXML) has the ability to set this when saving a workbook, however ClosedXML XLWorkbook.SaveAs(SaveOptions)
doesn't include this. Perhaps I can't find the closedxml documentation on this option, but I believe this is the option I need.
According to this answer and the attached github/MSDN links OpenXML doesn't support password protecting but I don't see any info about readonly open other than the first linked question saying it should be possible.
Code example:
XLWorkbook wb = new XLWorkbook();
var ws = wb.AddWorksheet("My Sheet");
// ... add to worksheet
ws.Protect("my-worksheet-password");
wb.SaveAs("C:\\my-workbook.xlsx", new SaveOptions { /* In OpenXML the option would exist during this save step. */ });