【问题标题】:Is there any way to Read Excel File that Receive via form-data in Azure Function (Version 2)有什么方法可以读取通过 Azure 函数中的表单数据接收的 Excel 文件(版本 2)
【发布时间】:2019-12-29 04:08:52
【问题描述】:

我需要开发 azure 函数来逐行读取 Excel 文件并将这些数据插入到数据库中

所以通过使用下面的代码,我能够从 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..");
}

我看到有很多方法可以在 c# 中读取 excel 表,例如

Microsoft.Office.Interop.Excel

ExcelMapper

OLEDB ..等

但问题是那些需要文件存储路径

问题是我可以在不写入 blob 或其他位置的情况下读取这些 excel 文件

谢谢

【问题讨论】:

    标签: c# excel asp.net-core azure-functions


    【解决方案1】:

    您可以尝试ExcelDataReader,使用流作为输入参数。假设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();
                }
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2021-01-24
    • 2021-09-12
    相关资源
    最近更新 更多