如何将Excel数据透视表中的“子项”转移到另一列?

我生成一个Excel工作表,其中包含数据格式如下:

在这里输入图像说明

IOW中,“Total Packages”,“Total Purchases”,“Average Price”以及“Total of Total”值均位于各自总体(或侧重)描述的自己的(Data)列中。

当我转换这些数据时,它将这些值放在每个描述下:

在这里输入图像说明

这是有道理的,但习惯于以前的外观的人希望它被复制到数据透视表中。 如何将数据透视表中的描述“子项目”转移到他们自己的列中?

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

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列字段下alignment。 我想将这四个字段向右移动到他们自己的列,并将描述标签在这四个值之间垂直居中放置在左边。 [这怎么可能?

尝试改变你的表格的布局

 pivotTable.RowAxisLayout xlTabularRow pivotTable.MergeLabels = True 

这是结果:

在这里输入图像说明

C#中的一个脚本与Interop.Excel。 包括使用;)

 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:\42\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); 

这是所有关于数据透视表布局/devise…这里的手动方式 – 萨尔瓦多有VBA方式:)…

数据透视表设计