简体   繁体   English

有什么方法可以读取通过 Azure 函数中的表单数据接收的 Excel 文件(版本 2)

[英]Is there any way to Read Excel File that Receive via form-data in Azure Function (Version 2)

I need to develop azure function for read Excel file row by row and insert those data in to database我需要开发用于逐行读取 Excel 文件的 azure 函数并将这些数据插入到数据库中

so by using below code i was able to get file from HTTP request所以通过使用下面的代码,我能够从 HTTP 请求中获取文件

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我看到有很多方法可以在 C# 中读取 excel 表,例如

Microsoft.Office.Interop.Excel微软.Office.Interop.Excel

ExcelMapper Excel映射器

OLEDB ..etc OLEDB ..等

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问题是我可以在不写入 blob 或其他位置的情况下读取这些 excel 文件吗

Thank you谢谢

You can try ExcelDataReader , using stream as input parameter.您可以尝试ExcelDataReader ,使用流作为输入参数。 Assuming that excel_file is byte array you can use code bellow:假设excel_file是字节数组,您可以使用以下代码:

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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM