C# 从excel文件中删除图形

C# 从excel文件中删除图形,c#,excel,C#,Excel,我这里有一个函数,它获取一个工作表并从中生成一个图形。我想从excel工作表中删除图表,保存后我尝试了.delete(),但这只是抛出了一个错误,我该怎么做呢 private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename) { string topLeft = ToCell(0, 0); string bottomRight

我这里有一个函数,它获取一个工作表并从中生成一个图形。我想从excel工作表中删除图表,保存后我尝试了.delete(),但这只是抛出了一个错误,我该怎么做呢

    private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename)
    {
        string topLeft = ToCell(0, 0);
        string bottomRight = ToCell(lastRow - 1, lastColumn - 1);

    worksheet.get_Range(ToCell(0, 0), missing).Formula = "Max(B2:" + bottomRight + ")";
    worksheet.get_Range(ToCell(0, 0), missing).FormulaHidden = true;
    worksheet.get_Range(ToCell(0, 0), missing).Calculate();
    Range range = (Range)worksheet.Cells[1,1];

    string small = (string)range.Value2;
    double min = Convert.ToDouble(small);
    worksheet.get_Range(ToCell(0,0),missing).Formula = "";

    //Generates the graph
    range = worksheet.get_Range(topLeft, bottomRight);
    ChartObjects Xlchart = (ChartObjects)worksheet.ChartObjects(missing);
    ChartObject chart = (ChartObject)Xlchart.Add(20, 160, 600, 500);
    Excel.Chart myChart = chart.Chart;
    myChart.SetSourceData(range, missing);

    //sets the y axis
    Axis axis = (Axis)myChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
    axis.MinimumScaleIsAuto = true;
    axis.MaximumScaleIsAuto = true;
    axis.HasTitle = true;
    axis.AxisTitle.Text = "Measure (m)";
    axis.CrossesAt = (int)(min-1);

    //sets the x axis
    Axis xAxis = (Axis)myChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
    xAxis.HasTitle = true;
    xAxis.AxisTitle.Text = "Position (m)";

    //makes the graph a line graph
    myChart.ChartType = XlChartType.xlXYScatterLinesNoMarkers;

    //titles the graph
    myChart.HasTitle = true;
    myChart.ChartTitle.Text = "Profiles";

    //saves the graph
    myChart.Export(filename, "JPG", missing);

    //
    //here is where I would like to delete the graph
    //
}

如果mychart.delete不起作用,请尝试mychart.parent.delete

不走运,父对象是object,没有对象的delete父对象是ChartObject,它有.delete方法。