Setting Password For Excel Spreadhsheet

Jun 29, 2016 at 5:38 AM
I am currently trying to set password for my generated excel from my web app
My source code are as below:
 protected void ExportBtn_Click(object sender, System.EventArgs e)
    ExcelPackage excel = new ExcelPackage();
    var workSheet = excel.Workbook.Worksheets.Add("test");

    using (var memoryStream = new MemoryStream())

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=Test.xlsx");

        excel.SaveAs(memoryStream , "Password");



I have tried using the above codes to generate a password protected excel spreadsheet, however after entering the correct password, I am faced with an error:

Excel cannot open the file 'filename.xlsx' because the file format for the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file

I have tried changing this line of source code and it works perfectly
Can anyone tell me what's wrong and how to generate a password protect excel spreadsheet. Any help will be greatly appreciated