【问题标题】:Linq to excel not working , when upgrading .net core 2.1 to .net core 2.2将 .net core 2.1 升级到 .net core 2.2 时,Linq to excel 不起作用
【发布时间】:2019-12-05 06:50:24
【问题描述】:

> 我的项目使用的是 .Net core 2.1。我用 LINQ 来excel得到

从excel文件中读取数据。当我通过我的项目升级到 .Net 核心 2.2.它不工作。

我从 excel 文件中读取数据的代码是

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();

//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;
        }
    }
}

但它现在不起作用,请给出一些解决方案。

【问题讨论】:

    标签: asp.net-core epplus linq-to-excel


    【解决方案1】:

    我认为 linq to excel 在 .net core 2.2 中不起作用。所以更好用 EPPlus 包。从 nuget 包下载 EPPlus.Core

    我遇到了同样的问题,我得到了解决方案。

    使用 OfficeOpenXml;

    使用它,我们读取每一行和每一列并将其放入我们的视图模型中。如下代码所示。

    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.
    
    
                            }
                        }
                    }
    

    【讨论】:

      【解决方案2】:

      这是 OleDb 驱动程序在 .Net Core 2.x 版本中不起作用的问题。它应该在 .Net Core 3.0 中得到修复(参见 GitHub issue

      您能否尝试升级到 .Net Core 3.0,看看 LinqToExcel 是否适合您。

      【讨论】:

      • 对不起,我需要切换到 .net core 2.2
      • LinqToExcel 不适用于 .net core 3.1.. 我正在采用上面给出的解决方案。令人遗憾的是,雄伟的 LINQ 方法现在将尘埃落定
      • 通过从 nuget 安装 oledb 设法解决了这个问题
      【解决方案3】:

      默认情况下,LinqToExcel 无法在 .Net Core 上运行,因为 OLEDB 包未移植到 dotnetcore。 但是,要解决您的问题,您需要安装一个单独的 package 特定于 OLE DB,并且可以在 Nuget 上下载和安装,这反过来将解决您在 LinqToExcel 上的其他问题,因为该包在内部使用此 OLEDB。

      来自 nuget 的包将安装以下内容:

      System.Data.OleDb.OleDbCommand
      System.Data.OleDb.OleDbCommandBuilder
      System.Data.OleDb.OleDbConnection
      System.Data.OleDb.OleDbDataAdapter
      System.Data.OleDb.OleDbDataReader
      System.Data.OleDb.OleDbParameter
      System.Data.OleDb.OleDbParameterCollection
      System.Data.OleDb.OleDbTransaction
      

      它们现在可在面向 .NET Core 3.1、.NET Framework 4.6.1、.NETStandard 2.0 和其他框架的 System.Data.OleDb Nuget 包中使用。

      另请参阅:导致此软件包开发和发布的 GitHub 问题 (https://github.com/dotnet/corefx/issues/23542)。

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 2021-05-14
        • 2020-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        • 1970-01-01
        相关资源
        最近更新 更多