在 C# 中,使用 Spire.Xls,在折叠行上方使用 collapse/expand 选项对 excel sheet 行进行分组

In C#, using Spire.Xls, Group excel sheet rows with collapse/expand option above collapsed rows

我已通过 E-Iceblue 教程在 C# 中创建 excel 组,但找不到在折叠行上方设置 collapse/expand 选项的选项。

为了对行进行分组,我使用了以下代码:

 Worksheet sheet = workbook.Worksheets[0];

        sheet.GroupByRows(2, 9, true);

但默认情况下,expand/collapse 图标位于折叠行下方的行旁边。 我需要在折叠行上方的行上使用此选项。 我知道这可以在 excel sheet 中手动完成。在 excel 2013 中,数据选项卡,大纲设置,取消选中 "Summary rows below detail" 后,collapse/expand 图标移动到详细信息上方。

我的问题是,我们如何使用 Spire.Xls 在 C# 代码中执行此操作?

编辑可以使用Spire.Xls完成。在E-iceblue上找到了解决方案,Spire.Xls , forum。下面的代码行完成这项工作。

sheet.PageSetup.IsSummaryRowBelow = false;

如果有人想通过 OpenXML 执行此操作,请参阅下面的原始答案。


无法使用 Spire.Xls 完成(在回答此问题时)。 可以使用 OpenXML 完成。

这里是 E-iceblue,Spire.Xls,论坛 link 这个主题。如论坛中所述,此功能可能会在未来的某个版本中出现。

对于遇到此主题的任何其他人,可以使用以下代码使用 OpenXML 完成此操作 (假设 excel sheet 代码的其余部分使用 Spire.Xls):

Workbook ReportWorkbook = new Workbook();

//Spire.Xls code here

//Save file
ReportWorkbook.SaveToFile("myExcel.xlsx", ExcelVersion.Version2010);

//Open the excel using OpenXML
using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument oXmlSheet =
    DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open("myExcel.xlsx", true))
{
    var oXmlWorkbook = oXmlSheet.WorkbookPart.Workbook;

    //Get the sheet property object for the first sheet
    var sp = oXmlWorkbook.WorkbookPart.WorksheetParts.ToList()[0].Worksheet.SheetProperties;

    //Initialize outline properties
    sp.OutlineProperties = new DocumentFormat.OpenXml.Spreadsheet.OutlineProperties();

    //Update sheet outline properties so that, 
    //the option to expand/collapse row groups is shown beside the row above the grouped rows.
    sp.OutlineProperties.SummaryBelow = false;

    //Save file
    oXmlWorkbook.Save();
}