Search code examples
asp.net-coreeppluslinq-to-excel

Linq to excel not working , when upgrading .net core 2.1 to .net core 2.2


enter image description here> I was using .Net core 2.1 for my project. I used LINQ to excel to get

read data from excel file. When i upgrade by my project to .Net core 2.2. it is not working.

my code to read data from excel file is

string pathToExcelFile = "path to excel file."
ExcelHelper ConxObject = new ExcelHelper(pathToExcelFile);

var query = from a in ConxObject.UrlConnexion.Worksheet<ExcelProcessFollowUp>()
            select a;

var data = query.ToList();

//Helper class for excel

public class ExcelHelper
{
    public string _pathExcelFile;
    public ExcelQueryFactory _urlConnexion;

    public ExcelHelper(string path)
    {
        this._pathExcelFile = path;
        this._urlConnexion = new ExcelQueryFactory(_pathExcelFile);
    }

    public string PathExcelFile
    {
        get
        {
            return _pathExcelFile;
        }
    }

    public ExcelQueryFactory UrlConnexion
    {
        get
        {
            return _urlConnexion;
        }
    }
}

but it is not working now please give some solution to it.


Solution

  • I think linq to excel don't work in .net core 2.2. so better to use EPPlus package. Download EPPlus.Core from nuget package

    i have faced the same problem and i got solution for this.

    using OfficeOpenXml;

    using this we read each row and column and put it to our viewmodel. as shown in below code.

    Dictionary<object, string> leadExcelViewModels = new Dictionary<object, string>();
    
              using (ExcelPackage package = new ExcelPackage(file))
                    {
                        ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
                        int totalRows = workSheet.Dimension.Rows;//get total rows counts of excel file
                        int totalColumns = workSheet.Dimension.Columns;// get total columns count of excel file.
    
                        if (totalRows > 1)
                        {
                            for (int i = 2; i < totalRows; i++)
                            {
                                leadExcelViewModels = new Dictionary<object, string>();
                                leadModel = new YourViewModel();
                                for (int j = 1; j <= totalColumns; j++)
                                {
                                    if (workSheet.Cells[i, j].Value != null)
                                        leadExcelViewModels.Add(workSheet.Cells[1, j].Value, workSheet.Cells[i, j].Value.ToString());
                                    else
                                        leadExcelViewModels.Add(workSheet.Cells[1, j].Value, "0");
                                }
    
                                var js = JsonConvert.SerializeObject(leadExcelViewModels);
                                leadModel = JsonConvert.DeserializeObject<YourViewModel>(js);
                               // bind each leadModel to List of your YourViewModel list.
    
    
                            }
                        }
                    }