简体   繁体   中英

How to group by row in excel using c#

i'm using c# in my project and i want to suceed to generate a excel file, but what i want is group the data in the excel file by item.PRJ_NAM, how can i do that ?

// les légendes
        ws.Cell("A1").Value = "Project Name";
        ws.Cell("B1").Value = "Version";
        ws.Cell("C1").Value = "Status";
        ws.Cell("D1").Value = "Record Date";
        ws.Cell("E1").Value = "Closing Date";
        ws.Cell("F1").Value = "Currency";

        // les données
        int current_line = 2;
        foreach (var item in results)
        {
            ws.Cell(current_line, 1).Value = item.PRJ_NAME;
            ws.Cell(current_line, 2).Value = item.VERSION_NUMBER;
            ws.Cell(current_line, 3).Value = item.STATUS_NAME;
            ws.Cell(current_line, 4).Value = item.my_RECORD_DATE;
            ws.Cell(current_line, 5).Value = item.my_DATE;
            ws.Cell(current_line, 6).Value = item.tata_LIB;

            current_line++;
        }

        workbook.SaveAs(mem);
        workbook.Dispose();
        return mem.ToArray();

I may be radically oversimplifying what you are trying to accomplish, but if I understand your ask, all you need to do is add a LINQ expression to order the results by the project name:

foreach (var item in results.OrderBy(x => x.PRJ_NAME))
{
    ws.Cell(current_line, 1).Value = item.PRJ_NAME;
    ws.Cell(current_line, 2).Value = item.VERSION_NUMBER;
    ws.Cell(current_line, 3).Value = item.STATUS_NAME;
    ws.Cell(current_line, 4).Value = item.my_RECORD_DATE;
    ws.Cell(current_line, 5).Value = item.my_DATE;
    ws.Cell(current_line, 6).Value = item.tata_LIB;

    current_line++;
}

If I have missed the boat, then I definitely concur with @PaulF -- some sample data and expected output would help get you the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM