browserblazor-webassemblyclosedxml

How to create and download an Excel workbook with "Enable Editing" using ClosedXml in a Blazor WASM app


A Google search yielded: To create a spreadsheet with "Enable Editing" using ClosedXML in C#, simply create a new workbook and add data to it without applying any protection mechanisms; by default, a new ClosedXML workbook allows editing in all cells.

I've done that, but when I open the downloaded spreadsheet it comes up in Protected View and requires that I hit an Enable Editing button.

I've looked at the ClosedXml documentation but didn't find (or understand) anything helpful, other than a statement that ClosedXML doesn’t support "Allow edit ranges" feature yet. Not sure if that applies to the entire workbook/worksheet.

The example given in the Google search uses workbook.SaveAs("file.xlsx"), which I can't do in a browser. Instead, I have to use workbook.SaveAs(stream) where stream is a MemoryStream that I then download as a byte array using javascript. Perhaps that's the issue, or maybe I missing something else. I tried adding "workbook.Unprotect()" to my code but that didn't change the outcome.

I'd appreciate any thoughts.


Solution

  • Protected View is not something you can avoid. It is a safety feature built into Excel and other Office products, driven by a Windows-level feature called Mark of the Web. Every Excel file downloaded from the Internet would get the same treatment, irrespective of the web framework technology used and how the Excel file was produced.