C# 如何使用c在excel中按行分组#

C# 如何使用c在excel中按行分组#,c#,excel,C#,Excel,我在我的项目中使用c#,我想成功生成一个excel文件,但我想要的是按item.PRJ#NAM将excel文件中的数据分组,我该怎么做 // les légendes ws.Cell("A1").Value = "Project Name"; ws.Cell("B1").Value = "Version"; ws.Cell("C1").Value = "Status"; ws.Cell("D1").Value = "Record D

我在我的项目中使用c#,我想成功生成一个excel文件,但我想要的是按item.PRJ#NAM将excel文件中的数据分组,我该怎么做

// 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();

我可能过于简单化了您试图实现的目标,但如果我理解您的要求,您所需要做的就是添加一个LINQ表达式,以按项目名称排序结果:

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

如果我错过了机会,那么我肯定同意@PaulF——一些示例数据和预期输出将帮助您找到解决方案。

您是指按PRJ_名称订购吗?如果您给出一些示例数据&您的预期输出,将会有所帮助。