将 .net core 2.1 升级到 .net core 2.2 时,Linq to excel 不工作
Linq to excel not working , when upgrading .net core 2.1 to .net core 2.2
> 我的项目使用的是 .Net core 2.1。我使用 LINQ to excel 得到
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;
}
}
}
但现在无法正常工作,请提供一些解决方案。
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.
使用 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.
}
}
}
这是 OleDb 驱动程序无法在 .Net Core 2.x 版本中运行的问题。它应该在 .Net Core 3.0 中得到修复(参见这个GitHub issue)
您可以尝试升级到 .Net Core 3.0 并查看 LinqToExcel 是否适合您。
默认情况下,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
它们现在在 System.Data.OleDb Nuget 包中可用,该包针对 .NET Core 3.1、.NET Framework 4.6.1、.NETStandard 2.0 和其他框架。
另请参阅:导致开发和发布此软件包的 GitHub 问题 (https://github.com/dotnet/corefx/issues/23542)。
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;
}
}
}
但现在无法正常工作,请提供一些解决方案。
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.
使用 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.
}
}
}
这是 OleDb 驱动程序无法在 .Net Core 2.x 版本中运行的问题。它应该在 .Net Core 3.0 中得到修复(参见这个GitHub issue)
您可以尝试升级到 .Net Core 3.0 并查看 LinqToExcel 是否适合您。
默认情况下,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
它们现在在 System.Data.OleDb Nuget 包中可用,该包针对 .NET Core 3.1、.NET Framework 4.6.1、.NETStandard 2.0 和其他框架。
另请参阅:导致开发和发布此软件包的 GitHub 问题 (https://github.com/dotnet/corefx/issues/23542)。