如何在EPPlus中将数据透视表报表布局设置为表格?

时间:2012-02-27 11:14:46

标签: epplus

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

2 个答案:

答案 0 :(得分:7)

事实证明这比我想象的要简单......通过将'everything'设置为'false',表格呈现为Tabular。所以基本上:

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;

这样我就可以获得带有表格布局的数据透视表。

答案 1 :(得分:4)

这是解决方案。将所有字段(包括隐藏字段)紧凑并将属性设置为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;