如何使用C# Interop EXCEL创建“测量”

问题描述 投票:0回答:1

请帮忙! // 错误

System.Runtime.InteropServices.COMException: '0x800A03EC'

Excel.PivotField newMeasure =calculatedFields.Add("NewMeasure", 公式, true);

AddMeasure(task.attachment.FullName, 
    WorkSheetIndex: GetWorksheetIndex(task, "Promo Voice total"), 
    pivotTableName: "pt1", 
    MeasuresName: "myesures", 
    formula: "=A1+B1");

public static void AddMeasure(string file, int WorkSheetIndex, string pivotTableName, string MeasuresName, string formula)
{
    Excel.Application excelApp = new Excel.Application();

    try
    {
        Excel.Workbook workbook = excelApp.Workbooks.Open(file);
        Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[WorkSheetIndex];

        Excel.PivotTable pivotTable = worksheet.PivotTables(pivotTableName) as Excel.PivotTable;

        if (pivotTable != null)
        {
            Excel.CalculatedFields calculatedFields = pivotTable.CalculatedFields();
            if (calculatedFields != null)
            {
                Excel.PivotField newMeasure = calculatedFields.Add("NewMeasure", formula, true);**// HERE ERROR `System.Runtime.InteropServices.COMException: '0x800A03EC'`**

                ((Excel.PivotField)pivotTable.PivotFields($"{MeasuresName}")).Orientation = Excel.XlPivotFieldOrientation.xlDataField;

                if (newMeasure != null)
                {
                    pivotTable.AddDataField(newMeasure);
                    Console.WriteLine($"Measure '{MeasuresName}' added successfully.");
                }}}
        workbook.Save();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"An error occurred: {ex.Message}");
    }
    finally
    {
        excelApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
    }
}

//错误

System.Runtime.InteropServices.COMException: '0x800A03EC'

Excel.PivotField newMeasure =calculatedFields.Add("NewMeasure", 公式, true);

c# excel interop measure
1个回答
0
投票

我很快在 VB.net 中编写了这段代码。它按预期工作。

之前:

代码:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xlApp As New Excel.Application
        Dim xlWb As Excel.Workbook
        Dim Mdl As Excel.Model
        Dim tbl As Excel.ModelTable

        With xlApp
            .Visible = True

            '~~> Open workbook
            xlWb = .Workbooks.Open("C:\Users\routs\Downloads\Test.xlsm")

            Mdl = xlWb.Model
            tbl = Mdl.ModelTables("Range")

            '~~> Delete the measure if it already exists
            Try
                Mdl.Model.ModelMeasures("NewMeasure").Delete
            Catch ex As Exception
            End Try

            Try
                xlWb.Model.ModelMeasures.Add("NewMeasure", tbl, "1+2", Mdl.ModelFormatDecimalNumber(False, 2))
            Catch ex As Exception
                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End With

        '~~> Save and Close workbook and quit Excel
        xlWb.Close(True)
        xlApp.Quit()

        '~~> Flush the toilet
        Kawoosh(tbl)
        Kawoosh(xlWb)
        Kawoosh(Mdl)
        Kawoosh(xlApp)
    End Sub

    Private Sub Kawoosh(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

输出

© www.soinside.com 2019 - 2024. All rights reserved.