在C#中,使用Spire.Xls,在折叠行上方使用折叠/展开选项对Excel表单行进行excel

时间:2016-12-08 12:17:09

标签: c# excel export-to-excel

我已经通过E-Iceblue教程在c#中创建excel组,但无法找到折叠/展开选项在折叠行上方的选项。

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

 Worksheet sheet = workbook.Worksheets[0];

        sheet.GroupByRows(2, 9, true);

但默认情况下,展开/折叠图标位于折叠行下方的行旁边。 我在折叠行上方的行上需要此选项。 我知道这可以在excel表中手动完成。在excel 2013中,数据选项卡,大纲设置,取消选中"摘要行以下详细信息"折叠/展开图标移动到细节上方。

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

1 个答案:

答案 0 :(得分:1)

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

sheet.PageSetup.IsSummaryRowBelow = false;

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

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

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

对于其他任何绊倒此主题的人来说,可以使用下面的代码使用OpenXML来完成(假设其余的excel表格代码是使用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();
}
相关问题