Getting the error: The type initializer for 'Excel.ExcelReaderFactory' threw an exception.
I am trying to download excel .xls (97 - 2003 format) from Azure Storage and open the file and load it into a dataset.
I have installed the Nuget: Install-Package ExcelDataReader v3.3.0, but received compile errors. So in addition I installed the excelDataReader-DevNet45 which complied successfully.
It seems I can connect and blockBlobReference.DownloadToStream(memoryStream); successfully.
But when I try to exec the following line, I received the error: The type initializer for 'Excel.ExcelReaderFactory' threw an exception.
(I also tried the CreateOpenXmlReader as well) Same error. I pasted my code in lower part of this post. Thank you for your help!
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(memoryStream);
private void ImportAzure()
{
try
{
string connectionString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
string sourceContainerName = "hsc2"; // ConfigurationManager.AppSettings["sourcecontainerName"]; //source blob container name
string sourceBlobFileName = FileUpload1.FileName; // "test.xlsx"; //source blob name
var excelData = GetExcelBlobData(sourceBlobFileName, connectionString, sourceContainerName);
RadGrid1.DataSource = excelData;
RadGrid1.DataBind();
}
catch (Exception ex)
{
Console.Write("Error: ImportAzure: " + ex.Message);
}
}
/// <summary>
/// GetExcelBlobData
/// Gets the Excel file Blob data and returns a dataset
/// </summary>
/// <param name="filename"></param>
/// <param name="connectionString"></param>
/// <param name="containerName"></param>
/// <returns></returns>
private static DataSet GetExcelBlobData(string filename, string connectionString, string containerName)
{
DataSet ds = null; ;
try
{
// 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 "test.xlsx"
CloudBlockBlob blockBlobReference = container.GetBlockBlobReference(filename);
using (var memoryStream = new MemoryStream())
{
//downloads blob's content to a stream
blockBlobReference.DownloadToStream(memoryStream);
/*
used open source Excel Data Reader - Read Excel files in .NET(http://exceldatareader.codeplex.com/)
Nuget: Install-Package ExcelDataReader (https://www.nuget.org/packages/ExcelDataReader/)
*/
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(memoryStream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
//IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream);
//var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream);
ds = excelReader.AsDataSet();
excelReader.Close();
}
return ds;
}
catch (Exception ex)
{
Console.Write("GetExcelBlobData Error: " + ex.Message);
return ds;
}
}