使用EPPLUS从列表导出到Excel [英] export to Excel from a list with EPPLUS

查看:876
本文介绍了使用EPPLUS从列表导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EPPLUS在C#中将列表导出到Excel中,当我执行该程序时不会给我错误,但是当我打开Excel时看到不正确的数据时,他输入了projet +对象名称与对象具有列表的次数相同:

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

提示是,当我在Visual Studio中调试应用程序时,我可以看到列表已正确填充:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆