如何将 Excel 数据透视表中的 "subitems" 移动到另一列?

How can I shift the "subitems" in an Excel PivotTable to another column?

我生成了一个 Excel sheet,其中包含格式如下的数据:

IOW,"Total Packages"、"Total Purchases"、"Average Price" 和“占总计的百分比”值位于它们自己的列(数据)中,用于每个总体(或侧向) ) 描述。

当我对这些数据进行数据透视表时,它会将这些值置于每个描述下方:

这是有道理的,但那些习惯了以前的外观的人希望它在数据透视表中被复制。如何将数据透视表中的描述 "subitems" 移动到它们自己的列中?

这是我用来生成数据透视表的代码:

private void PopulatePivotTableSheet()
{
    string NORTHWEST_CORNER_OF_PIVOT_TABLE = "A6";
    AddPrePivotTableDataToPivotTableSheet();
    var dataRange = rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address];
    dataRange.AutoFitColumns();
    var pivotTable = pivotTableWorksheet.PivotTables.Add(
                        pivotTableWorksheet.Cells[NORTHWEST_CORNER_OF_PIVOT_TABLE], 
                        dataRange, 
                        "PivotTable");
    pivotTable.MultipleFieldFilters = true;
    pivotTable.GridDropZones = false;
    pivotTable.Outline = false;
    pivotTable.OutlineData = false;
    pivotTable.ShowError = true;
    pivotTable.ErrorCaption = "[error]";
    pivotTable.ShowHeaders = true;
    pivotTable.UseAutoFormatting = true;
    pivotTable.ApplyWidthHeightFormats = true;
    pivotTable.ShowDrill = true;

    // Row field[s]
    var descRowField = pivotTable.Fields["Description"];
    pivotTable.RowFields.Add(descRowField);

    // Column field[s]
    var monthYrColField = pivotTable.Fields["MonthYr"];
    pivotTable.ColumnFields.Add(monthYrColField);

    // Data field[s]
    var totQtyField = pivotTable.Fields["TotalQty"];
    pivotTable.DataFields.Add(totQtyField);

    var totPriceField = pivotTable.Fields["TotalPrice"];
    pivotTable.DataFields.Add(totPriceField);

    // Don't know how to calc these vals here, so have to grab them from the source data sheet
    var avgPriceField = pivotTable.Fields["AvgPrice"];
    pivotTable.DataFields.Add(avgPriceField);

    var prcntgOfTotalField = pivotTable.Fields["PrcntgOfTotal"];
    pivotTable.DataFields.Add(prcntgOfTotalField);
}

所以有一个RowField("MonthYr"),值为“201509”和“201510”,一个ColumnField("Description")和四个DataField,它们在Description列字段下对齐它们.我想将这四个字段向右移动到它们自己的列,并将描述标签在这四个值之间垂直居中放置在它们的左侧。 [如何]这可能吗?

尝试将 table 的布局更改为

pivotTable.RowAxisLayout xlTabularRow
pivotTable.MergeLabels = True

这是结果:

带有 Interop.Excel 的 C# 小脚本。包括使用 ;)

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

var excelApp = new Excel.Application();
Excel.Workbook wb = excelApp.Workbooks.Open(@"e:\TestSO.xlsx");
Worksheet ws = wb.Worksheets["SheetName"];
PivotTable pt = ws.PivotTables("DynamicTableName");
pt.RowAxisLayout(XlLayoutRowType.xlTabularRow);
pt.MergeLabels = true;
wb.Save();
wb.Close();
Marshal.ReleaseComObject(ws);

一切都与数据透视表布局/设计有关...这是手动方式 - Salvador 有 VBA 方式 :)...