使用 EPPLUS 从列表导出到 Excel [英] export to Excel from a list with EPPLUS
问题描述
我正在尝试使用 EPPLUS 在 c# 中将列表导出到 Excel,当我执行程序时不会给我错误,但是当我打开 Excel 时,我看到不是正确的数据,他输入了项目+对象的名称与对象具有列表的次数一样多:
i´m trying to export a list to Excel in c# with EPPLUS, when i execute the program don´t give me errors, but when i open the Excel i see that are not the correct data, he put the name of the projet+the name of the object as many times as objects have the list:
对象代码:
class Stock
{
public string Nif;
public string Proveedor;
public string Coodigo;
public string descripcion;
public string Catalogo;
public string Estadistico;
public decimal StockOn;
}
当这些列表(lstStock)被填满时,我创建一个 Excel 并使用选项 loadfromcollection :
and when thes list(lstStock) is filled i create an Excel and use the option loadfromcollection :
System.IO.FileInfo f = new System.IO.FileInfo("D:\stock_termos.xlsx");
if (f.Exists) f.Delete();
using (ExcelPackage ep = new ExcelPackage(f))
{
ExcelWorksheet hoja = ep.Workbook.Worksheets.Add("TOTAL OBSOLETOS");
hoja.Cells[1, 1].Value = "NIF"; ;
hoja.Cells[1, 2].Value = "Proveedor";
hoja.Cells[1, 3].Value = "Código";
hoja.Cells[1, 4].Value = "Descripción";
hoja.Cells[1, 5].Value = "Catálogo";
hoja.Cells[1, 6].Value = "Cod.Estadístico";
hoja.Cells[1, 7].Value = "Stock On";
hoja.Cells[2, 1].LoadFromCollection(lstStock);
}
提示是,当我在 VisualStudio 中调试应用程序时,我可以看到列表已正确填充:
The cuestión is that when i debug the aplication in VisualStudio i can see the list is correctly filled:
所以我认为错误是当我尝试使用 LoadFromCollection 方法将数据导出到 Excel 时,但我不知道出了什么问题,请帮忙.
So i think the error is when i try to export the data to Excel, with the LoadFromCollection method, but i can´t se what is wrong, please help.
推荐答案
您使用的是什么版本的 EPPlus?我问是因为我很惊讶它不会像当前最新的 4.1.0 那样抛出错误.也许旧版本更宽容.
What version of EPPlus are you using? I ask because I am surprised it does not throw an error as it does with 4.1.0 which is currently the latest. Maybe an older version is more forgiving.
但是为了回答您的问题,如果您查看最终调用的 LoadFromCollection
的最终重载的签名,您会看到:
But to answer you question, if you look at the signature of the final overload of LoadFromCollection
that is eventually called you will see this:
public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members)
请注意,Epplus 仅查看 MemberInfos
而不是您对象正在使用的 Fields
.如果您将 Stock
对象更改为:
Notice that Epplus is only looking at MemberInfos
and not a Fields
which is what you object is using. If you change Stock
object to this:
class Stock
{
public string Nif { get; set; }
public string Proveedor { get; set; }
public string Coodigo { get; set; }
public string descripcion { get; set; }
public string Catalogo { get; set; }
public string Estadistico { get; set; }
public decimal StockOn { get; set; }
}
您应该会看到结果.
这篇关于使用 EPPLUS 从列表导出到 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!