如何在图表上设置SourceData。收到错误:“从COM组件的调用返回了HRESULT E_FAIL”

时间:2019-03-29 11:40:26

标签: c# .net excel pivot

我正在尝试在数据透视表上创建两个图表。当我创建第一个时,可以,但是当我尝试创建第二个时,出现以下错误:“从对COM组件的调用中返回了HRESULT E_FAIL”

String propFile  = "./connection.properties";

按照下面的代码告诉大家我在做什么

chartPage.SetSourceData(oPivotTable.TableRange2, misValue);

我希望这可以正常工作

Workbook xlWorkBook = xlApp.Workbooks.Add (rootFile + "Template.xlsm");
Worksheet xlWorkSheet = (Worksheet) xlWorkBook.Worksheets.get_Item (1);

if (!Directory.Exists (rootFile)) {
    Directory.CreateDirectory (rootFile);
}

string fileName = rootFile + report.Name + ".xlsm";

xlWorkSheet.Name = "Invoices";

List<Sheet> sheets = JsonConvert.DeserializeObject<List<Sheet>> (report.Definition);

var startCell = (Range) xlWorkSheet.Cells[1, 1];
var endCell = (Range) xlWorkSheet.Cells[invoices.Count () + creditNotes.Count (), properties.Count () + 1];
var writeRange = xlWorkSheet.Range[startCell, endCell];

writeRange.Value = dataSource;

int lastROW = xlWorkSheet.Cells[xlWorkSheet.Rows.Count, 1].End (XlDirection.xlUp).Row;
int LastCol = xlWorkSheet.Cells[1, xlWorkSheet.Columns.Count].End (XlDirection.xlToLeft).Column;
Range pRange = xlWorkSheet.Cells[1, 1].Resize (lastROW, LastCol);

pRange.Columns.AutoFit ();

foreach (var sheet in sheets) {
    PivotCache oPivotCache = (PivotCache) xlWorkBook.PivotCaches ().Add (XlPivotTableSourceType.xlDatabase, pRange);
    xlWorkBook.ShowPivotChartActiveFields = true;

    Worksheet xlWorkSheetPivot = (Worksheet) xlWorkBook.Worksheets.Add (misValue);
    xlWorkSheetPivot.Name = sheet.tabHeading;
    xlWorkSheetPivot.Activate ();

    foreach (Pivot pivot in sheet.pivots) {
        Range last = GetRange (xlWorkSheetPivot);

        PivotTable oPivotTable = (PivotTable) xlWorkSheetPivot.PivotTables ().Add (PivotCache: oPivotCache, TableDestination: last, TableName: pivot.title);
        oPivotTable.SortUsingCustomLists = false;

        foreach (var filter in pivot.filters) {
            PivotField oPivotFieldrow1 = (PivotField) oPivotTable.PivotFields (filter.displayName);
            oPivotFieldrow1.EnableMultiplePageItems = true;

            oPivotFieldrow1.Orientation = XlPivotFieldOrientation.xlPageField;
            oPivotFieldrow1.Name = filter.displayName;

            if (!String.IsNullOrEmpty (filter.value)) {
                oPivotFieldrow1.CurrentPage = filter.value;
            }
        }

        foreach (var row in pivot.rows) {
            PivotField oPivotFieldrow1 = (PivotField) oPivotTable.PivotFields (row.displayName);
            oPivotFieldrow1.Orientation = XlPivotFieldOrientation.xlRowField;
            oPivotFieldrow1.LayoutCompactRow = true;
            oPivotFieldrow1.LayoutForm = XlLayoutFormType.xlOutline;
            oPivotFieldrow1.Name = row.displayName;
        }

        foreach (var column in pivot.columns) {
            PivotField oPivotFieldrow1 = (PivotField) oPivotTable.PivotFields (column.displayName);
            oPivotFieldrow1.Orientation = XlPivotFieldOrientation.xlColumnField;
            oPivotFieldrow1.LayoutCompactRow = true;
            oPivotFieldrow1.LayoutForm = XlLayoutFormType.xlOutline;
            oPivotFieldrow1.Name = column.displayName;
        }

        var functionToOrder = "";

        foreach (var value in pivot.values) {
            PivotField field = (PivotField) oPivotTable.PivotFields (value.displayName);
            field.Orientation = XlPivotFieldOrientation.xlDataField;
            field.NumberFormat = "0.00";

            if (value.function != null)
                field.Function = GetEnumExcel (value.function.name);

            field.Caption = System.Text.RegularExpressions.Regex.Replace (field.Caption, @"[\d-]", string.Empty);
            field.Name = System.Text.RegularExpressions.Regex.Replace (field.Name, @"[\d-]", string.Empty);
            functionToOrder = field.Name;
        }

        if (pivot.pivotType == "Chart") {
            ChartObjects xlCharts = (ChartObjects) xlWorkSheetPivot.ChartObjects (Type.Missing);

            ChartObject myChart = (ChartObject) xlCharts.Add (oPivotTable.TableRange2.Left + 350, last.Top, 400, 200);

            Chart chartPage = myChart.Chart;
            chartPage.SetSourceData (oPivotTable.TableRange2, misValue);
            chartPage.ChartType = (XlChartType) pivot.chartType.id;
        }

        if (pivot.values.Count > 1) {
            oPivotTable.DataPivotField.Orientation = XlPivotFieldOrientation.xlColumnField;
        }

        //Order By Fields
        if (pivot.fieldToOrder != null && pivot.orderType != 0 && (!string.IsNullOrEmpty (pivot.fieldToOrder.displayName) || pivot.fieldToOrder.function != null)) {
            PivotField pivotField = oPivotTable.PivotFields (pivot.fieldToOrder.displayName);

            var fieldToOrder = pivot.fieldToOrder.function == null ? pivot.fieldToOrder.displayName : pivot.fieldToOrder.function.name + " of " + pivot.fieldToOrder.displayName;

            xlApp.Run ("UseOrderFunction", sheet.tabHeading, pivot.title, pivot.rows.FirstOrDefault ().displayName, pivot.orderType, fieldToOrder);
        }

        oPivotTable.RowAxisLayout (XlLayoutRowType.xlTabularRow);

        if (pivot.topCount.HasValue && pivot.topCount > 0) {
            var fieldToUseTop = pivot.fieldToOrder.function == null ? functionToOrder : pivot.fieldToOrder.function.name + " of " + pivot.fieldToOrder.displayName;

            xlApp.Run ("UseTopFunction", sheet.tabHeading, pivot.title, pivot.rows.FirstOrDefault ().displayName, pivot.topCount, pivot.orderType, fieldToUseTop);
        }
    }
}

0 个答案:

没有答案