6

查看 EPPlus 附带的示例,我设法创建了数据透视表,但我无法为其设置正确的报告布局。我希望它是“表格”,而不是“大纲”或其他什么。在我看来,EPPlus 现在不支持这个,但也许我错过了什么?

4

2 回答 2

8

事实证明这比我想象的要容易......通过将“一切”设置为“假”,表格呈现为表格。所以基本上:

pivotTable.Compact = false;
pivotTable.CompactData = false;
pivotTable.Indent = 0;
pivotTable.RowGrandTotals = false;
pivotTable.UseAutoFormatting = true;
pivotTable.ShowMemberPropertyTips = false;
pivotTable.DataOnRows = false;

field.Outline = false;
field.Compact = false;
field.ShowAll = false;
field.SubtotalTop = false;

这样我就得到了表格布局的数据透视表。

于 2012-03-01T11:02:55.587 回答
6

这是解决方案。将所有字段(包括隐藏字段)的紧凑和大纲属性显式设置为 false。

(from pf in pivot.Fields
             select pf).ToList().ForEach(f =>
                 {
                     f.Compact = false;
                     f.Outline = false;
                 });

只是有一些上下文,这是我生成枢轴的代码。它使用元数据,但概念是相同的。

        var pivotTabName = "Pivot";
        if (tab.Form.FormTabs.Count(ft => ft.FormTabPivotFields.Any()) > 1)
            pivotTabName = tab.DisplayName + " " + pivotTabName;

        var sheet = package.Workbook.Worksheets.Add(pivotTabName);

        const int pivotRow = 7;
        const int pivotCol = 1;

        var dataSourceRange = table.WorkSheet.Workbook.Names[table.Name + "_PivotSource"];
        var pivot = sheet.PivotTables.Add(sheet.Cells[pivotRow, pivotCol], dataSourceRange.Worksheet.Cells[dataSourceRange.Address],
                              pivotTabName.Replace(" ", string.Empty));

        const bool outline = false;
        const bool compact = false;
        const bool showAll = false;

        pivot.Compact = compact;
        pivot.CompactData = compact;
        pivot.Outline = outline;
        pivot.OutlineData = outline;
        pivot.Indent = 0;
        pivot.UseAutoFormatting = true;
        pivot.ShowMemberPropertyTips = false;
        pivot.DataOnRows = false;
        pivot.RowGrandTotals = false;
        pivot.ShowDrill = false;
        pivot.EnableDrill = false;
        pivot.RowGrandTotals = false;
        pivot.ColumGrandTotals = true;
        pivot.MultipleFieldFilters = true;

        (from pf in tab.FormTabPivotFields
         where pf.PivotFieldType.Name == "Page"
         orderby pf.DisplayOrder
         select pf.FormTabFactVcTypeDomain != null ? pf.FormTabFactVcTypeDomain.DisplayColumnName : pf.FormTabMeasureTypeColumn.DisplayColumnName).ToList().ForEach(fieldName =>
             {
                 var fld = pivot.PageFields.Add(pivot.Fields[fieldName]);
                 fld.Compact = compact;
                 fld.Outline = outline;
                 fld.ShowAll = showAll;
                 fld.SubtotalTop = false;
                 fld.SubTotalFunctions = eSubTotalFunctions.None;
             });

        (from pf in tab.FormTabPivotFields
         where pf.PivotFieldType.Name == "Row"
         orderby pf.DisplayOrder
         select pf.FormTabFactVcTypeDomain != null ? pf.FormTabFactVcTypeDomain.DisplayColumnName : pf.FormTabMeasureTypeColumn.DisplayColumnName).ToList().ForEach(fieldName =>
         {
             var fld = pivot.RowFields.Add(pivot.Fields[fieldName]);
             fld.Compact = compact;
             fld.Outline = outline;
             fld.ShowAll = showAll;
             fld.SubtotalTop = false;
             fld.SubTotalFunctions = eSubTotalFunctions.None;
         });

        (from pf in tab.FormTabPivotFields
         where pf.PivotFieldType.Name == "Column"
         orderby pf.DisplayOrder
         select pf.FormTabFactVcTypeDomain != null ? pf.FormTabFactVcTypeDomain.DisplayColumnName : pf.FormTabMeasureTypeColumn.DisplayColumnName).ToList().ForEach(fieldName =>
         {
             var fld = pivot.ColumnFields.Add(pivot.Fields[fieldName]);
             fld.Compact = compact;
             fld.Outline = outline;
             fld.ShowAll = showAll;
             fld.SubtotalTop = false;
             fld.SubTotalFunctions = eSubTotalFunctions.None;
         });

        (from pf in tab.FormTabPivotFields
         where pf.PivotFieldType.Name == "Data"
         orderby pf.DisplayOrder
         select new
             {
                 FieldName = pf.FormTabFactVcTypeDomain != null ? pf.FormTabFactVcTypeDomain.DisplayColumnName : pf.FormTabMeasureTypeColumn.DisplayColumnName,
                 Format = pf.FormTabMeasureTypeColumn != null ?
                    (pf.FormTabMeasureTypeColumn.MeasureFormatIndex == 1 ? pf.FormTab.MeasureColumnExcelNumberFormat1 : pf.FormTab.MeasureColumnExcelNumberFormat2)
                    : "General"
             }).ToList().ForEach(md =>
             {
                 var df = pivot.DataFields.Add(pivot.Fields[md.FieldName]);
                 df.Format = md.Format;
                 df.Field.Compact = compact;
                 df.Field.Outline = outline;
             });

        (from pf in pivot.Fields
             where pf.Axis == ePivotFieldAxis.None && pf.IsDataField == false
             select pf).ToList().ForEach(f =>
                 {
                     f.Compact = compact;
                     f.Outline = outline;
                 });

        // apply pivot table styling
        pivot.TableStyle = TableStyles.Medium15;
于 2013-04-05T20:40:33.433 回答