qiuhao zeng
qiuhao zeng

Reputation: 11

How to use Interop.Excel to create multi pivot chart in C#?

foreach (var item in itemlist.Items)
{
    if (item.PivotChartConfig != null)
    {
        var describedPivotChart = item;
        var chartConfig = describedPivotChart.PivotChartConfig;

        Excel.PivotCache pivotCache = workbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, dataRange);

        Excel.Range pivotTableLocation;

        if (lastInsertedRange != null)
        {
            pivotTableLocation = pivotSheet.Cells[lastInsertedRange.Row + lastInsertedRange.Rows.Count + 1, 1];
        }
        else
        {
            pivotTableLocation = pivotSheet.Cells[1, 1];
        }

        Excel.PivotTable pivotTable = pivotCache.CreatePivotTable(
            TableDestination: pivotTableLocation,
            TableName: "PivotTable" + Guid.NewGuid().ToString());   

        ConfigurePivotTableFields(pivotTable, chartConfig.PivotTableConfig.Fields);
        Excel.Range pivotTableRange = pivotTable.TableRange2;

        double chartLeft = pivotTableRange.Left + pivotTableRange.Width + 10;  
        double chartTop = pivotTableRange.Top;
        double chartWidth = 500;   
        double chartHeight = 300; 

        Excel.ChartObjects chartObjects = (Excel.ChartObjects)pivotSheet.ChartObjects();
        Excel.ChartObject chartObject = chartObjects.Add(
            chartLeft,
            chartTop,
            chartWidth,
            chartHeight);

        Excel.Chart chart = chartObject.Chart;
        chart.SetSourceData(pivotTable.TableRange2);

        chart.ChartType = chartConfig.ChartType;
        chart.HasTitle = true;
        chart.ChartTitle.Text = chartConfig.ChartTitle;
        lastInsertedRange = pivotTable.TableRange2;

        if (!string.IsNullOrEmpty(describedPivotChart.Description))
        {
            pivotTableRange = pivotTable.TableRange2;
            int descriptionRow = pivotTableRange.Row + pivotTableRange.Rows.Count;
            int descriptionColumn = pivotTableRange.Column;

            Excel.Range descriptionCell = pivotSheet.Cells[descriptionRow, descriptionColumn];
            descriptionCell.Value2 = describedPivotChart.Description;

            descriptionCell.Font.Bold = true;
            descriptionCell.Font.Italic = true;
            descriptionCell.EntireRow.AutoFit();
            lastInsertedRange = pivotSheet.Cells[descriptionRow, descriptionColumn];
        }
    }
    else
    {
        if (lastInsertedRange == null)
        {
            lastInsertedRange = pivotSheet.Cells[1, 1];
        }

        int stringItemRow = lastInsertedRange.Row + lastInsertedRange.Rows.Count + (lastInsertedRange == pivotSheet.Cells[1, 1] ? 0 : 1);
        int stringItemColumn = lastInsertedRange.Column;

        Excel.Range stringItemCell = pivotSheet.Cells[stringItemRow, stringItemColumn];
        stringItemCell.Value2 = item.Description;

        stringItemCell.Font.Bold = true;
        stringItemCell.Font.Size = 12;
        stringItemCell.EntireRow.AutoFit();

        lastInsertedRange = pivotSheet.Cells[stringItemRow, stringItemColumn];
    }
}

If only one pivot chart is created, my code runs normally, but if multiple pivot charts are created, an error occurs.

Unhandled exception. System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.

Error occurs on this line of code:

chart.SetSourceData(pivotTable.TableRange2);

I reviewed Microsoft's interop documentation but couldn't find a suitable solution.

Upvotes: 1

Views: 24

Answers (0)

Related Questions