6

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

2
  • You could just google it and it would give you numerous solutions. Here is one: msdn.microsoft.com/en-us/library/7czw9wzs.aspx (Keep in mind that you will need to add the ref to the excel lib in your project) And here is a duplicate: stackoverflow.com/questions/915999/… Commented May 20, 2015 at 6:10
  • It looks like you can't password protect it (or remove password protection) with ClosedXML, but @NoelWidmer 's links should be sufficient from protecting it after you've created it with ClosedXML. Commented Jun 17, 2015 at 20:28

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

this is possible in PowerShell?
This doesn't work for me. The Excel file opens without requesting a password.
THIS IS NOT WORKING FOR ME
3

To protect the file with a password is not possible in ClosedXML, see github

5 Comments

Downvote because it is possible, see @JesseZ answer.
check my link, it's not possible, JesseZ answer doesn't work.
maybe we're talking about different things because JesseZ's answer 100% worked for me. That's for password protecting a worksheet from editing, not the entire file from opening. Is that what you're talking about?
Yes I'm talking about password protecting the entire file from opening.
+1 because, @Cerveser, you're absolutely correct. You can protect individual sheets from being edited, but you cannot password protect the document from being opened with ClosedXML. The original question did not specify which type of protection they wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.