如何使用C#删除Excel图表图例条目?

如何使用C#删除Excel图表图例条目?,c#,export-to-excel,office-interop,excel-interop,C#,Export To Excel,Office Interop,Excel Interop,我想删除Excel图表中部分(但不是全部)系列的图例条目。根据我的经验,似乎SeriesCollection.Item(index)和LegendEntries.Item(index)没有关系。给定一个系列n如何仅删除该系列的图例 我正在将Office Interop 2010与Visual Studio 2010一起使用。通过GUI选择图例条目,然后右键单击并选择“删除”即可轻松完成此操作。要删除图例条目,您必须知道要删除的图例的索引。不幸的是,似乎不存在通过InteropAPI公开legen

我想删除Excel图表中部分(但不是全部)系列的图例条目。根据我的经验,似乎
SeriesCollection.Item(index)
LegendEntries.Item(index)
没有关系。给定一个系列
n
如何仅删除该系列的图例


我正在将Office Interop 2010与Visual Studio 2010一起使用。通过GUI选择图例条目,然后右键单击并选择“删除”即可轻松完成此操作。

要删除图例条目,您必须知道要删除的图例的索引。不幸的是,似乎不存在通过InteropAPI公开legend和series之间关系的关系。然而,有一个关键的解决办法。要删除特定系列的图例,我采用的方法是在添加系列后立即删除图例。这是唯一一次已知图例索引

// .
// . code to add series
// .
// remove the legend entry for the newly added series
chart.Legend.LegendEntries(chart.Legend.LegendEntries().Count).Delete();

要删除图例条目,您必须知道要删除的图例的索引。不幸的是,似乎不存在通过InteropAPI公开legend和series之间关系的关系。然而,有一个关键的解决办法。要删除特定系列的图例,我采用的方法是在添加系列后立即删除图例。这是唯一一次已知图例索引

// .
// . code to add series
// .
// remove the legend entry for the newly added series
chart.Legend.LegendEntries(chart.Legend.LegendEntries().Count).Delete();

我知道最初的问题是关于OfficeInterop2010的,但这似乎也是在图表中动态显示和隐藏系列的一个很好的参考点,因此我将添加以下顶部,以防它对其他人有所帮助

如果要在Office 2013中的工作表上显示/隐藏图表对象上的系列(至少在VBA中;不确定是否为互操作),可以执行以下操作:

Worksheets("MySheetName").ChartObjects("MyChartName").Chart.FullSeriesCollection("MyLedendSeriesName").IsFiltered = false

这将隐藏序列而不删除它。将其设置为true以再次显示序列。

我知道最初的问题是关于Office Interop 2010的,但这似乎也是在图表中动态显示和隐藏序列的一个很好的参考点,因此我将添加以下顶部,以防对其他人有所帮助

如果要在Office 2013中的工作表上显示/隐藏图表对象上的系列(至少在VBA中;不确定是否为互操作),可以执行以下操作:

Worksheets("MySheetName").ChartObjects("MyChartName").Chart.FullSeriesCollection("MyLedendSeriesName").IsFiltered = false

这将隐藏序列而不删除它。将其设置为true以再次显示序列。

我需要从图表中删除最后两个图例条目,因为它们是为创建十字线效果而添加的伪序列。一个系列画垂直线,另一个画水平线。删除图例条目后,该系列仍保留在图表上,这正是我想要的。我使用了下面的代码:

Microsoft.Office.Interop.Excel.ChartObject chartObj = null;
Microsoft.Office.Interop.Excel.Chart chart = null;
Microsoft.Office.Interop.Excel.Legend legend = null;
Microsoft.Office.Interop.Excel.LegendEntries legendEntries = null;
Microsoft.Office.Interop.Excel.LegendEntry legendItem;

int legendEntryCount = 0;

chartObj = (Microsoft.Office.Interop.Excel.ChartObject) xlws.ChartObjects("Chart 1");
chart = chartObj.Chart;
legend = chart.Legend;
legendEntries = (Microsoft.Office.Interop.Excel.LegendEntries) chart.Legend.LegendEntries();
legendEntryCount = legendEntries.Count;
if (legendEntryCount > 2)
{
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount);
    legendItem.Delete();
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount - 1);
    legendItem.Delete();
}

我需要从图表中删除最后两个图例条目,因为它们是为创建十字线效果而添加的伪系列。一个系列画垂直线,另一个画水平线。删除图例条目后,该系列仍保留在图表上,这正是我想要的。我使用了下面的代码:

Microsoft.Office.Interop.Excel.ChartObject chartObj = null;
Microsoft.Office.Interop.Excel.Chart chart = null;
Microsoft.Office.Interop.Excel.Legend legend = null;
Microsoft.Office.Interop.Excel.LegendEntries legendEntries = null;
Microsoft.Office.Interop.Excel.LegendEntry legendItem;

int legendEntryCount = 0;

chartObj = (Microsoft.Office.Interop.Excel.ChartObject) xlws.ChartObjects("Chart 1");
chart = chartObj.Chart;
legend = chart.Legend;
legendEntries = (Microsoft.Office.Interop.Excel.LegendEntries) chart.Legend.LegendEntries();
legendEntryCount = legendEntries.Count;
if (legendEntryCount > 2)
{
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount);
    legendItem.Delete();
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount - 1);
    legendItem.Delete();
}