[英]Receive file with servicestack from multipart/form-data
[英]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.