Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create password protected excel file using Closedxml library

Tags:

.net

I am new in c# asp.net and I tried to create XLS file using third party library "Closedxml". Now I also want to protect the XLS file with password how I can achieve this.

Your help would be appreciable. Thanks in advance Ashish

like image 505
Ashish Singla Avatar asked May 20 '15 06:05

Ashish Singla


People also ask

What is ClosedXML Excel?

ClosedXML is a . NET library for reading, manipulating and writing Excel 2007+ (. xlsx, . xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.


1 Answers

Had the same issue - needed to protext an excel file without using the interop libraries.

With ClosedXML you can password protect at the worksheet level: 
var ws = wb.Worksheets.Add("someworksheet");
ws.Protect("123");  // 123 will be the password

And it looks like you can lock the structure of the workbook (prevent worksheet add/delete/rename) without the option of setting a password, although have not tested this bit extensively:

var wb = new XLWorkbook();
wb.Protect(bool LockStrucure, bool LockWindows); 

references: sheet protection example

like image 166
JesseZ Avatar answered Sep 21 '22 13:09

JesseZ