SetSourceData对图表返回HRESULT E_FAIL对数据透视表(Excel c#)

本文关键字:透视 Excel 数据 返回 HRESULT SetSourceData FAIL | 更新日期: 2023-09-27 18:09:06

我想在一个工作表中创建几个数据透视表,并从每个数据透视表中创建一个图表。第一个数据透视表和第一个图表正常创建。在我创建第二个数据透视表之后,我想用第二个数据透视表的源添加第二个图表,但是这会返回HRESULT E_FAIL,并且第二个图表具有第一个数据透视表的源,我无法更改。

        string pitpivotNam1 = "table1";
        Dictionary<String, Excel.PivotField> pFields = new Dictionary<String, Excel.PivotField>();
        Excel.PivotTable pivotTable1 = null;
        Excel.Range pivotDest = excelApp.Worksheets["PivotTable"].Cells[1, 1];
        excelApp.Worksheets[sheetData].Activate();
        int countColmn = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
        var headerNam = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn]].Value;
        // Data Source for PivotTable
        Excel.Range pivotData = excelApp.Worksheets[sheetData].usedRange;
        Excel.PivotCache pivotcache = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
        excelApp.Worksheets[sheetDest].Activate();
        pivotTable1 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache, pivotDest, pitpivotNam1, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
        // Create Dynamically PivotFields
        for (int i = 1; i <= headerNam.Length - 1; i++)
        {
            pFields.Add("test" + i.ToString(), (Excel.PivotField)pivotTable1.PivotFields(i));
        }
        pFields["test20"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields["test20"].Function = Excel.XlConsolidationFunction.xlAverage;
        pFields["test43"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields["test43"].Function = Excel.XlConsolidationFunction.xlAverage;
        pFields["test66"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields["test66"].Function = Excel.XlConsolidationFunction.xlAverage;
        pFields["test20"].Name = "DVP & R Progress";
        // First Chart
        excelApp.ActiveSheet.Shapes.AddChart.Select();
        excelApp.ActiveChart.SetSourceData(pivotTable1.TableRange1);
        // Second PivotTable
        string pitpivotNam2 = "table2";
        Excel.PivotTable pivotTable2 = null;
        Dictionary<String, Excel.PivotField> pFields2 = new Dictionary<String, Excel.PivotField>();
        Excel.Range pivotDest2 = excelApp.Worksheets["PivotTable"].Cells[20, 1];
        excelApp.Worksheets[sheetData].Activate();
        int countColmn2 = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
        var headerNam2 = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn2]].Value;
        // Data Source for PivotTable
        Excel.Range pivotData2 = excelApp.Worksheets[sheetData].usedRange;
        Excel.PivotCache pivotcache2 = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData2.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
        excelApp.Worksheets[sheetDest].Activate();
        pivotTable2 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache2, pivotDest2, pitpivotNam2, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
        // Create Dynamically PivotFields
        for (int i = 1; i <= headerNam2.Length - 1; i++)
        {
            pFields2.Add("test" + i.ToString(), (Excel.PivotField)pivotTable2.PivotFields(i));
        }
        pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
        Excel.PivotField dataField = pivotTable2.DataPivotField;
        pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields2["test17"].Function = Excel.XlConsolidationFunction.xlCount;
        pFields2["test40"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields2["test40"].Function = Excel.XlConsolidationFunction.xlCount;
        pFields2["test63"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
        pFields2["test63"].Function = Excel.XlConsolidationFunction.xlCount;
        dataField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
        // Secound Chart
        excelApp.ActiveSheet.Shapes.AddChart.Select();
        excelApp.ActiveChart.SetSourceData(pivotTable2.TableRange1);

事先感谢您的帮助。

问候,丹尼尔

SetSourceData对图表返回HRESULT E_FAIL对数据透视表(Excel c#)

我在一个VSTO c#项目中遇到过类似的问题。

在我的例子中,我要做的是在创建图表之前选择枢轴范围。

我发现,如果在创建图表时选择了第一个数据透视的一个单元格,则图表将绑定到该数据透视,并且无法更改源数据(事实上,即使在Excel中也不能这样做,图表的源范围是灰色的)。

我通过在创建图表之前选择透视单元格来解决。

Range rangePivot2 = pivotTable2.TableRange1;
rangePivot2.Select();
Chart chart = sheet.Controls.AddChart(cellsWhereTheChartWillBeDrawn, "secondChart");
chart.SetSourceData(rangePivot2); /* this won't give you E_FAIL... but it's not really needed */

我的代码示例是VSTO伪代码,但我相信类似的方法也适用于纯Excel COM自动化。