饼图中的百分比格式C#

时间:2012-06-21 13:05:38

标签: c# excel-interop

我使用Microsoft Office Interop Excel在VS 2008 C#Project中创建饼图。

代码是:

Excel.Chart xlChart = (Excel.Chart)xlWorkBook
        .Charts
        .Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);                

Excel.Range xlDataRange = xlWorkSheet
        .get_Range(xlWorkSheet.Cells[
                       nDataForDiagramRow - alToExcelInvestmentGroupNames.Count,
                       _nColumnsCount - 3
                   ],
                   xlWorkSheet.Cells[
                       nDataForDiagramRow - 1, 
                       _nColumnsCount - 2
                   ]);

xlChart.ChartType = Excel.XlChartType.xl3DPieExploded;
xlChart.Elevation = 60;
xlChart.Rotation = 130;

xlChart.SetSourceData(xlDataRange, Type.Missing);
xlChart.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowLabelAndPercent,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing);

xlChart.Location(Excel.XlChartLocation.xlLocationAsObject, xlWorkSheet.Name);

使用方法xlChart.ApplyDataLabels我可以看到标签和百分比,但百分比是圆的。

如何访问标签格式以显示百分比的小数部分?

1 个答案:

答案 0 :(得分:0)

我自己创建了解决方案。 nLabel是图表中标签集合中的标签的int号。

Excel.Series xlSeries = (Excel.Series)xlWorkBook.ActiveChart.SeriesCollection(1);

Excel.DataLabel dl = (Excel.DataLabel)xlSeries.DataLabels(nLabel);

dl.NumberFormat = "0.0%";