0

I want to read an excel file stored in an Azure Storage container as a Blob using Epplus package in C#. I have tried to do something with this code.

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());

It's throwing an error?

Abdul
  • 176
  • 3
  • 19

1 Answers1

3

We cannot use Azure blob url to initialize the FileInfo object, it will throw the error System.NotSupportedException: 'The given path's format is not supported.'.

enter image description here

So if you want to read excel file stored in Azure blob, we need to download it at first.

For example

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

enter image description here

HackSlash
  • 4,944
  • 2
  • 18
  • 44
Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • I get my stream fine, then I pass it in the new ExcelPackage(stream) and the package is created fine and I see his Stream property filled. But when I try to obtain the Worksheets I get the error System.NullreferenceExceptio worksheet was null – kintela Mar 24 '22 at 18:04