Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

c# - export to Excel from a list with EPPLUS

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: enter image description here

The code of the object:

class Stock
        {
            public string Nif;
            public string Proveedor;
            public string Coodigo;
            public string descripcion;
            public string Catalogo;
            public string Estadistico;
            public decimal StockOn;

        }

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

The cuestión is that when i debug the aplication in VisualStudio i can see the list is correctly filled:

enter image description here

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)

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

You should see results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...