gpt4 book ai didi

c# - 如何使用 C# 在 Excel 中添加数据透视图?

转载 作者:太空狗 更新时间:2023-10-30 01:25:33 25 4
gpt4 key购买 nike

如何根据工作表中已创建的表格数据创建数据透视图?

最佳答案

尝试这样的事情:

            Microsoft.Office.Interop.Excel.PivotTable pivotCache = Globals.ThisAddIn.Application.ActiveWorkbook.PivotCaches().
Create(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, hoursTable2, XlPivotTableVersionList.xlPivotTableVersion12).
CreatePivotTable("PivotTable!R1C1", "PivotTable1", Type.Missing, XlPivotTableVersionList.xlPivotTableVersion12);
Microsoft.Office.Interop.Excel.Shape myChart = lPivotWorksheet.Shapes.AddChart();
myChart.Chart.SetSourceData(pivotCache.TableRange1, Type.Missing);

Globals.ThisAddIn.Application.ActiveWorkbook.ShowPivotChartActiveFields = true;

Microsoft.Office.Interop.Excel.PivotField rowField = (Microsoft.Office.Interop.Excel.PivotField)pivotCache.PivotFields("FieldTitle1");
rowField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;

pivotCache.AddDataField(pivotCache.PivotFields("FieldTitle2"), "Sum of Difference", Microsoft.Office.Interop.Excel.XlConsolidationFunction.xlSum);

Microsoft.Office.Interop.Excel.PivotField pageField = (Microsoft.Office.Interop.Excel.PivotField)pivotCache.PivotFields("FieldTitle3");
pageField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;

myChart.ScaleHeight(2, Microsoft.Office.Core.MsoTriState.msoFalse, Type.Missing);
myChart.ScaleWidth(2, Microsoft.Office.Core.MsoTriState.msoFalse, Type.Missing);
myChart.Left = 200;
myChart.Top = 120;
myChart.Chart.ChartTitle.Caption = "Chart Title";

关于c# - 如何使用 C# 在 Excel 中添加数据透视图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6671542/

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