Azure function app and ExcelDataReader

Dave Gray 586 Reputation points
2022-02-15T14:39:37.18+00:00

Hi,

I'm rewriting an old Winforms app that processes .XLS files with ExcelDataReader as a new Azure function fired by a blob trigger.

The old code basically allowed the user to browse for a file, opened as a FileStream object then read the spreadsheet using ExcelReaderFactory.CreateBinaryReader.

In the new Azure function / blob trigger the Run() method accepts a Stream, which ExcelReaderFactory (I thought) would handle, however I get an error.

*System.NotSupportedException: 'No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'*  
  

I have added the NuGet System.Text.Encoding.CodePages package but cant seem to add a the correct "using" statement.

174520-qa8.png

So I was wondering how to handle a Stream for use by ExcelDataReader. Would I convert to a CloudBlob but then I need it as a Stream (or derived type)?

Also, I will need to do some blob manipulation later in my code, mainly renaming. Will I need to cast my stream as a CloudBlob to do this manipulation.

 [StorageAccount("BlobConnectionString")]  
        public class LoadExcel  
        {  
            [FunctionName("LoadExcel")]  
            public void Run([BlobTrigger(blobPath: "workload/Inbound/{name}.{ext}")]   
                    Stream myBlob,  string name, string ext,  ILogger log)  
            {  
                Console.WriteLine($"-------> name = {name}, ext = {ext}");  
                Console.WriteLine();  
      
                if (ext.ToUpper().Equals("XLS"))  
                {  
                    Console.WriteLine($"object type of {name} is {myBlob.GetType()}");  
      
      
                    var s0 = ExcelReaderFactory.CreateReader(myBlob);  
                    var s1 = ExcelReaderFactory.CreateBinaryReader(myBlob);  
                    var s2 = ExcelReaderFactory.CreateOpenXmlReader(myBlob);  
      
                    //var fs = myBlob as CloudBlockBlob;  
      
                    //var type = typeof(Stream);  
      
                    //using (var reader = ExcelReaderFactory.CreateBinaryReader(myBlob))  
                    //{  
                    //    while (reader.Read())  
                    //    {  
                    //        Console.WriteLine(reader.GetDouble(0));  
                    //    }  
                    //}  
                }  
            }  
        }  
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.