如何使用Epplus C#在同一轮廓中添加多个折叠

时间:2019-09-13 14:43:03

标签: c# excel epplus

我需要在同一轮廓级别进行多次折叠。

 for (var j = 0; j <= 2; j++)
  {
    workSheet.Row(j).OutlineLevel = 1;
    workSheet.Row(j).Collapsed = true;
    workSheet.Row(j+3).OutlineLevel = 1;
    workSheet.Row(j+3).Collapsed = true;
  }

是否有可能在同一轮廓中实现两次折叠。 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我认为在Excel中做到这一点的唯一方法(暂时将Epplus搁置一旁)是在组之间创建一个间隙。在Epplus中,您可以执行以下操作:

[TestMethod]
public void Row_Multiple_Grouping_Test()
{
    //https://stackoverflow.com/questions/57925761/how-to-add-multiple-collapse-in-same-outline-using-epplus-c-sharp

    //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] = $"Header {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\Row_Multiple_Grouping_Test.xlsx");
    if (fi.Exists)
        fi.Delete();

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

        //Create the grouping
        for (var i = 2; i <= 11; i++)
        {
            worksheet.Row(i).OutlineLevel = 1;
            worksheet.Row(i).Collapsed = true;
        }

        //Create a gap - cant shrink or hide it because it hides the collapse button in GUI
        worksheet.InsertRow(7, 1);

        pck.Save();
    }
}

哪个给:

enter image description here

问题是您无法缩小或隐藏该行而不松开“展开/折叠”按钮。最好的办法是在各个组的页眉/页脚中放一个。