C# code read an excel content from blob

Rajnilari2015
Posted by Rajnilari2015 under Azure category on | Points: 40 | Views : 200
The below code will do so

public DataSet GetExcelBlobData(string filename, string connectionString, string containerName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);

// Retrieve reference to a blob named "myblob.csv"
CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(filename);

DataSet ds;
using (var memoryStream = new MemoryStream())
{
blockBlob2.DownloadToStream(memoryStream);
/*

used third party open source Excel Data Reader - Read Excel files in .NET(http://exceldatareader.codeplex.com/)
Nuget: Install-Package ExcelDataReader (https://www.nuget.org/packages/ExcelDataReader/)

*/

var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream);
ds = excelReader.AsDataSet();
excelReader.Close();
}

return ds;
}

Comments or Responses

Login to post response