Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过读取excel.NET CORE创建对象列表_C#_.net_Linq - Fatal编程技术网

C# 通过读取excel.NET CORE创建对象列表

C# 通过读取excel.NET CORE创建对象列表,c#,.net,linq,C#,.net,Linq,我有一个.xlsx文件,如下所示: 我需要阅读此文件并创建customers类的列表,其中name字段对应于第一列,email字段对应于第二列。以下是类别客户: namespace ReadExcelAndInsertMySQL.Domain.Entities { public class Customers { public string Name { get; set; } public string Email { get; set; }

我有一个.xlsx文件,如下所示:

我需要阅读此文件并创建customers类的列表,其中name字段对应于第一列,email字段对应于第二列。以下是类别客户:

namespace ReadExcelAndInsertMySQL.Domain.Entities
{
    public class Customers
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }
}
我正在尝试以下操作,但实际上,创建了一个列表,其大小与行数对应,但重复了电子表格的最后一个元素:

using OfficeOpenXml;
using ReadExcelAndInsertMySQL.Domain.Entities;
using ReadExcelAndInsertMySQL.Infra.Data.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var fi = new FileInfo(@"D:\\Arquivos\\clientes.xlsx");
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using (var package = new ExcelPackage(fi))
            {
                var workbook = package.Workbook;
                var worksheet = workbook.Worksheets.First();
                int colCount = worksheet.Dimension.End.Column;  //get Column Count
                int rowCount = worksheet.Dimension.End.Row;
                List<Customers> customers = new List<Customers>();
                Customers customer = new Customers();
                
                for (int row = 2; row <= rowCount; row++)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        if (col == 1)
                        {
                            customer.Name = worksheet.Cells[row, col].Value?.ToString().Trim();
                        }
                        else
                        {
                            customer.Email = worksheet.Cells[row, col].Value?.ToString().Trim();
                        }                        
                    }

                    customers.Add(customer);
                }
                customers.ToList().ForEach(x =>
                {
                    Console.WriteLine(x);
                });
            }
        }
    }
}
使用OfficeOpenXml;
使用ReadExcelAndInsertMySQL.Domain.Entities;
使用ReadExcelAndInsertMySQL.Infra.Data.Extensions;
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
var fi=新文件信息(@“D:\\Arquivos\\clientes.xlsx”);
ExcelPackage.LicenseContext=LicenseContext.NonCommercial;
使用(var程序包=新的ExcelPackage(fi))
{
var工作簿=package.workbook;
var worksheet=workbook.Worksheets.First();
int colCount=worksheet.Dimension.End.Column;//获取列计数
int rowCount=工作表.Dimension.End.Row;
列出客户=新列表();
客户=新客户();

对于(int row=2;row您有这个问题,因为您在循环行之前初始化了客户一次,要纠正这个问题,您应该为每一行初始化客户,如下代码所示:

...
for (int row = 2; row <= rowCount; row++)
{
    Customers customer = new Customers();
    for (int col = 1; col <= colCount; col++)
    {
        if (col == 1)
        {
            customer.Name = worksheet.Cells[row, col].Value?.ToString().Trim();
        }
        else
        {
            customer.Email = worksheet.Cells[row, col].Value?.ToString().Trim();
        }
    }

    customers.Add(customer);
}
customers.ToList().ForEach(x =>
{
    Console.WriteLine($"{x.Name}, {x.Email}");
});
...
。。。
对于(int row=2;row