Protect a Workbook with Password

This demo uses the DevExpress Spreadsheet Document API to password-protect workbook (the password grants access to workbook modification operations). You can convert the sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down.

Specify the protection password. Change settings in the Protection Settings panel, if required. Use the Protect and Save as... dropdown button to select the output format, protect the workbook, and download the result.

Sample document
InvestmentPortfolio.xlsx



using DevExpress.Spreadsheet;

Stream ProtectWorkbookWithPassword(Stream inputStream, DocumentFormat outputFormat, string workbookPassword, bool lockStructure, bool lockWindows) {
    using var workbook = new Workbook();

    workbook.LoadDocument(inputStream);

    if(!workbook.IsProtected) {
        workbook.Protect(workbookPassword ?? string.Empty, lockStructure, lockWindows);
    }

    var outputStream = new MemoryStream();

    workbook.SaveDocument(outputStream, outputFormat);

    outputStream.Position = 0;
    return outputStream;
}