如何对EPPlus中的行/列进行分组

时间:2021-01-29 22:30:34

Is there a way to achieve this in EPPlus? Only thing I could find on the internet is grouping specific data for example:

在EPPlus中有实现这一点的方法吗?在互联网上我能找到的唯一的东西是对特定的数据进行分组,例如:

AAA     --->    AAA     5 occurrences
AAA             BBB     2 occurences
BBB
BBB    
AAA
AAA
AAA

but not visually like in the screenshots

但在视觉上不像在截图中

1 个解决方案

#1


14  

Looks like you want to do Row and Columns outlines that are collapsed. This should demonstrate how to do that:

看起来你想要做的是折叠的行和列的轮廓。这应该说明如何做到这一点:

[TestMethod]
public void Row_Col_Grouping_Test()
{
    //http://*.com/questions/32760210/how-to-group-rows-columns-in-epplus

    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Header", typeof (string)), new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (object))
    });

    for (var i = 0; i < 10; i++)
    {
        var row = datatable.NewRow();
        row[0] = String.Format("Header {0}", i); row[1] = i; row[2] = i*10; row[3] = Path.GetRandomFileName(); datatable.Rows.Add(row);
    }

    //Create a test file
    var fi = new FileInfo(@"c:\temp\grouping.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (var pck = new ExcelPackage(fi))
    {
        var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
        worksheet.Cells.LoadFromDataTable(datatable, true);

        worksheet.Cells["B12"].Formula = "SUM(B2:B11)";
        worksheet.Cells["C12"].Formula = "SUM(C2:C11)";

        //Row Group 1
        for (var i = 2; i <= 6; i++)
        {
            worksheet.Row(i).OutlineLevel = 1;
            worksheet.Row(i).Collapsed = true;
        }

        //Row Group 2
        for (var i = 6; i <= 10; i++)
        {
            worksheet.Row(i).OutlineLevel = 2;
            worksheet.Row(i).Collapsed = true;
        }

        //Column Group
        for (var i = 2; i <= 4; i++)
        {
            worksheet.Column(i).OutlineLevel = 1;
            worksheet.Column(i).Collapsed = true;
        }

        pck.Save();
    }
}

#1


14  

Looks like you want to do Row and Columns outlines that are collapsed. This should demonstrate how to do that:

看起来你想要做的是折叠的行和列的轮廓。这应该说明如何做到这一点:

[TestMethod]
public void Row_Col_Grouping_Test()
{
    //http://*.com/questions/32760210/how-to-group-rows-columns-in-epplus

    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Header", typeof (string)), new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (object))
    });

    for (var i = 0; i < 10; i++)
    {
        var row = datatable.NewRow();
        row[0] = String.Format("Header {0}", i); row[1] = i; row[2] = i*10; row[3] = Path.GetRandomFileName(); datatable.Rows.Add(row);
    }

    //Create a test file
    var fi = new FileInfo(@"c:\temp\grouping.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (var pck = new ExcelPackage(fi))
    {
        var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
        worksheet.Cells.LoadFromDataTable(datatable, true);

        worksheet.Cells["B12"].Formula = "SUM(B2:B11)";
        worksheet.Cells["C12"].Formula = "SUM(C2:C11)";

        //Row Group 1
        for (var i = 2; i <= 6; i++)
        {
            worksheet.Row(i).OutlineLevel = 1;
            worksheet.Row(i).Collapsed = true;
        }

        //Row Group 2
        for (var i = 6; i <= 10; i++)
        {
            worksheet.Row(i).OutlineLevel = 2;
            worksheet.Row(i).Collapsed = true;
        }

        //Column Group
        for (var i = 2; i <= 4; i++)
        {
            worksheet.Column(i).OutlineLevel = 1;
            worksheet.Column(i).Collapsed = true;
        }

        pck.Save();
    }
}