0

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);
            }
slyp93
  • 46
  • 7
  • 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 https://stackoverflow.com/questions/44978634/access-protected-excel-file-with-exceldatareader-and-epplus – Nick.Mc Sep 28 '21 at 02:52
  • 1
    Thanks Nick, I solved this yesterday and used that approach. Yup using EPPlus. On Azure I was able to create the file in a temp folder and download the blob to the file I created on the temp folder. – slyp93 Sep 28 '21 at 13:41

1 Answers1

1

Solved by saving to a temp folder on Azure and then opening the file.

//Create temp path
string tempPath = Path.GetTempPath();
tempFullPath = tempPath + fileName;

 //Download blob
 blobClient.DownloadTo(tempFullPath);
slyp93
  • 46
  • 7