首页 > 解决方案 > Azure Blob 存储:从 Excel 工作簿中删除密码

问题描述

我在 Azure Blob 存储中保存了一个受密码保护的 Excel 工作簿,我想删除密码并将文件上传回 Blob。我编写了代码来密码保护 blob 中的 excel 文件,但我是 C# 新手,将密码保护文件作为流打开会产生错误。

有没有人成功地从保存在 Azure Blob 存储中的 excel 文件中删除密码?

//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);
            }

标签: excelazureblobeppluspassword-protection

解决方案


通过保存到 Azure 上的临时文件夹然后打开文件来解决。

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

 //Download blob
 blobClient.DownloadTo(tempFullPath);

推荐阅读