簡體   English   中英

有什么方法可以讀取通過 Azure 函數中的表單數據接收的 Excel 文件(版本 2)

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

我需要開發用於逐行讀取 Excel 文件的 azure 函數並將這些數據插入到數據庫中

所以通過使用下面的代碼,我能夠從 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 表,例如

微軟.Office.Interop.Excel

Excel映射器

OLEDB ..等

但問題是那些需要文件存儲路徑

問題是我可以在不寫入 blob 或其他位置的情況下讀取這些 excel 文件嗎

謝謝

您可以嘗試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();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM