This project has moved and is read-only. For the latest updates, please go here.

Setting Password For Excel Spreadhsheet

Jun 29, 2016 at 6:38 AM
I am currently trying to set password for my generated excel from my ASP.net 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.Clear();
        Response.ClearHeaders();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=Test.xlsx");


        excel.SaveAs(memoryStream , "Password");

        memoryStream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.End();
    }

}

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
 excel.SaveAs(memoryStream);
Can anyone tell me what's wrong and how to generate a password protect excel spreadsheet. Any help will be greatly appreciated