1

I need to develop azure function for read Excel file row by row and insert those data in to database

so by using below code i was able to get file from HTTP request

public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{

    var excel_file = req.Form.Files["file"];

    // read file and insert data and to database

    return excel_file != null
        ? (ActionResult)new OkObjectResult(excel_file)
        : new BadRequestObjectResult("err..");
}

i saw there are many ways to read excel sheet in c# such as

Microsoft.Office.Interop.Excel

ExcelMapper

OLEDB ..etc

but the problem is those are need the file stored path

problem is can i read these excel file without writing in a blob or other location

Thank you

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
Dulanga Heshan
  • 1,335
  • 1
  • 19
  • 36

1 Answers1

1

You can try ExcelDataReader, using stream as input parameter. Assuming that excel_file is byte array you can use code bellow:

using (var stream = new MemoryStream(excel_file))
{
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        while (reader.Read())
        {
            for (var i = 0; i < reader.FieldCount; i++)
            {
                var value = reader.GetValue(i)?.ToString();
            }
        }
    }
}
Roman.Pavelko
  • 1,555
  • 2
  • 15
  • 18
  • if any one get these System.NotSupportedException: No data is available for encoding 1252 exception refer https://stackoverflow.com/questions/49215791/vs-code-c-sharp-system-notsupportedexception-no-data-is-available-for-encodin – Dulanga Heshan Dec 29 '19 at 06:37