在C#中一次更改Excel图表的DataLabel

时间:2017-01-08 04:15:37

标签: c# excel charts fonts format

我使用以下代码更改C#中Excel图表的数据标签

using Xl = Microsoft.Office.Interop.Excel;
using officeState = Microsoft.Office.Core.MsoTriState;
....


for (int i = 0; i < rows.Count; i++)
{
    series1 = (Xl.Series)xlChart.SeriesCollection(i + 1);
    for (int k = 0; k < cols.Count; k++)
    {
        Xl.DataLabel dl = series1.DataLabels(k + 1) as Xl.DataLabel;
        dl.Font.Bold = officeState.msoTrue;
        dl.Font.Size = 11;
        dl.Font.Name = "Times New Roman";
    }

}

是否可以一次更改所有datalabel字体? 感谢

1 个答案:

答案 0 :(得分:0)

这就是我如何将系列中的数据标签加粗。我用条形图和饼图对它进行了测试,效果很好。

var series = chartObject.Chart.SeriesCollection() as Microsoft.Office.Interop.Excel.SeriesCollection;
foreach (var ser in series)
{
    var DataLabels = ((Microsoft.Office.Interop.Excel.Series)ser).DataLabels();
    DataLabels.Format.TextFrame2.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoTrue;
}