简体   繁体   English

在Angular中导入excel文件并将数据传入ASP.NET Core API项目

[英]Import excel file in Angular and pass data into ASP.NET Core API project

So the situation is the following.所以情况如下。 I have an Angular front end project and I want to import an excel file from the front end together with some additional properties.我有一个 Angular 前端项目,我想从前端导入一个 excel 文件以及一些其他属性。

The front end I will handle, but I have a couple of questions for the back end:前端我会处理,但我对后端有几个问题:

  1. What should I be receiving as a variable type in the back end for the excel file?我应该在后端接收什么作为 excel 文件的变量类型?
  2. I want to foreach the data from the excel file and get only specific data.我想从excel文件中获取数据并仅获取特定数据。 For example, I have one sheet only with rows Name, Age and Number of transactions.例如,我只有一张纸,其中包含行名称、年龄和交易数量。 I want to take only the name and number of transactions and save that information in a new instance of a class I have within my application.我只想获取交易的名称和数量,并将该信息保存在我的应用程序中的类的新实例中。

How do I do that?我怎么做?

required before you start the OPEN XML SDK在启动OPEN XML SDK之前需要

First in asp you need to defined your method with a IFormFile variable or use the FileUpload Helper class首先在 asp 中,您需要使用 IFormFile 变量定义您的方法或使用 FileUpload Helper 类

then you need to move the data into a stream eg IFormFile.CopyToAsync(Stream)那么你需要将数据移动到一个流中,例如 IFormFile.CopyToAsync(Stream)

finally open the spreadsheet for read only access so you can read the data最后打开电子表格进行只读访问,以便您可以读取数据

this would look something like this这看起来像这样

public async Task<IActionResult> OnPostUploadAsync(List<IFormFile> files)
{
    foreach(var file in files)
    {
        using var mem = new MemoryStream();
        await file .CopyToAsync(mem);
        var doc = SpreadsheetDocument Open(mem, false);
        ...

this will open the file and parse the data in it as an excel spreadsheet这将打开文件并将其中的数据解析为 Excel 电子表格

you can then use the spreadsheet methods to extract the data you require from inside the document然后,您可以使用电子表格方法从文档内部提取所需的数据

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

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