如何读取存储在 Azure 存储中的 excel 文件作为 Blob 文件

How to read an excel file stored in Azure Storage as a Blob File

我想使用 C# 中的 Epplus 包读取作为 Blob 存储在 Azure 存储容器中的 excel 文件。 我试图用这段代码做一些事情。

string uri = blob.Uri.AbsoluteUri;
FileInfo fileInfo = new FileInfo(uri);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
Console.WriteLine(ws.Cells[1,1].Value.ToString());

它抛出错误?

我们不能使用 Azure blob url 来初始化 FileInfo 对象,它会抛出错误 `System.NotSupportedException: '不支持给定路径的格式。'。

所以如果你想读取存储在Azure blob中的excel文件,我们需要先下载它。 例如

string connectionString = "";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");
            BlobClient blobClient = containerClient.GetBlobClient("sample.xlsx");
            
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using (var stream = await blobClient.OpenReadAsync(new BlobOpenReadOptions(true)))
            using (ExcelPackage package = new ExcelPackage(stream))
            {
                //get the first worksheet in the workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
                int colCount = worksheet.Dimension.End.Column;  //get Column Count
                int rowCount = worksheet.Dimension.End.Row;     //get row count
                for (int row = 1; row <= rowCount; row++)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
                    }
                }
               
            }




        }