gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:59:38 27 4
gpt4 key购买 nike

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

enter image description here

IOW、“总包”、“总购买”、“平均价格”和“总百分比”值位于它们自己的列(数据)中,用于每个总体(或侧面)描述。

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

enter image description here

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

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

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

这是结果:

enter image description here

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

关于excel - 如何将 Excel 数据透视表中的 "subitems"移动到另一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40289460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com