0
votes

I have a password-protected excel workbook saved in Azure Blob storage and I would like to remove the password and upload the file back to the blob. I wrote code to password protect an excel file in the blob but I am new to C# and opening the password protected file as a stream generates an error.

Has anyone had success removing the password from an excel file saved in Azure Blob storage?

//Open Excel on blob
            BlobServiceClient blobServiceClient = new BlobServiceClient(appsetting);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            //Password protect file
            using (var stream = await blobClient.OpenReadAsync(new BlobOpenReadOptions(true)))
            using (ExcelPackage package = new ExcelPackage(stream))
            {
                //Save password protected file
                package.Save(password);
                MemoryStream ms = new MemoryStream(package.GetAsByteArray());
                ms.Position = 0;

                //Delete the unprotected excel file
                blobClient.DeleteIfExists();

                //Upload password protected excel file
                BlobClient outputBlob = containerClient.GetBlobClient(fileName);
                outputBlob.Upload(ms);
            }
It's probably easier to download the file locally then use standard Excel methods to remove the password. You have EPPlus in your tags - are you using that? Here's a sample of opening password protected file using EPPlus stackoverflow.com/questions/44978634/… - Nick.McDermaid