Search code examples
azure-blob-storageazure-storageepplus

How to read data from Excel File saved in an blob in azure with EPPlus library


I'm trying to read my excel files saved in my azure storage container like this

  string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");          

  BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

  BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("concursos");

  foreach (BlobItem blobItem in containerClient.GetBlobs())
  {     

    BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);

    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

    using (var stream=blobClient.OpenRead(new BlobOpenReadOptions(true)))
    using (ExcelPackage package = new ExcelPackage(stream))
    {
     ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
     int colCount = worksheet.Dimension.End.Column;  
     int rowCount = worksheet.Dimension.End.Row;
     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());
        }
    }

But the sentence

ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();

throws me an error:System.NullReferenceException: 'Object reference not set to an instance of an object.' worksheet was null

I debug an see fine my stream an my package

debug

The excels in blobs are like this one .xls

excels

Any idea, please?

Thanks


Solution

  • The problem was the file extension of the excel files in blobs

    Only works fone with .xlsx not with .xls

    Thanks