将数据标签添加到excel饼图

时间:2022-05-11 15:21:47

I am drawing a pie chart with some data:

我正在绘制一个包含一些数据的饼图:

private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
{
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
        Excel.Chart chartPage = myChart.Chart;

        Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
        Excel.Series series1 = seriesCollection.NewSeries();
        series1.XValues = activeSheet.Range["E1","E3"];
        series1.Values = activeSheet.Range["F1","F3"];

        chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true,true,false,true,true,true,true);
        chartPage.ChartType = Excel.XlChartType.xlDoughnut;

        Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;
}

I just can't figure out how to turn on data labels. I googled everywhere for it but nothing's been helpful so far sadly.

我只是想不通如何打开数据标签。我到处搜索谷歌,但遗憾的是没有任何帮助。

3 个解决方案

#1


6  

Try this (TRIED AND TESTED)

试试这个(经过试验和测试)

    private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
    {
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
        Excel.Chart chartPage = myChart.Chart;

        Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
        Excel.Series series1 = seriesCollection.NewSeries();
        series1.XValues = activeSheet.Range["E1", "E3"];
        series1.Values = activeSheet.Range["F1", "F3"];

        chartPage.ChartType = Excel.XlChartType.xlDoughnut;

        Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;

        series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
    }

One quick question though. If you are not using xRange and yRange then why declare it?

但是一个简单的问题。如果你没有使用xRange和yRange那么为什么要声明呢?

This is the completed code that is tried and tested.

这是经过试验和测试的完整代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Excel.Application();

            xlexcel.Visible = true;
            // Add a Workbook
            xlWorkBook = xlexcel.Workbooks.Add();

            // Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 5] = "Apples";
            xlWorkSheet.Cells[2, 5] = "Oranges";
            xlWorkSheet.Cells[3, 5] = "Pears";

            xlWorkSheet.Cells[1, 6] = "80";
            xlWorkSheet.Cells[2, 6] = "65";
            xlWorkSheet.Cells[3, 6] = "45";

            Excel.ChartObjects myCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);

            // Specified xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6] just for the heck of it.
            DrawFractionChart(xlWorkSheet, myCharts, xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6]);

            //Once done close and quit Excel
            //xlWorkBook.Close(true, misValue, misValue);
            //xlexcel.Quit();

            //releaseObject(xlWorkSheet);
            //releaseObject(xlWorkBook);
            //releaseObject(xlexcel);
        }

        private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
        {
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
            Excel.Chart chartPage = myChart.Chart;

            Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
            Excel.Series series1 = seriesCollection.NewSeries();
            series1.XValues = activeSheet.Range["E1", "E3"];
            series1.Values = activeSheet.Range["F1", "F3"];

            chartPage.ChartType = Excel.XlChartType.xlDoughnut;

            Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;

            series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
        }

        //private void releaseObject(object obj)
        //{
        //    try
        //    {
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        //        obj = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        obj = null;
        //        MessageBox.Show("Unable to release the Object " + ex.ToString());
        //    }
        //    finally
        //    {
        //        GC.Collect();
        //    }
        //} 
    }
}

SNAPSHOT

快照

将数据标签添加到excel饼图

#2


2  

            #region Export Excel To Chart

            Excel.Range chartRange;

            Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(300, 50, 500, 500);
            Excel.Chart chartPage = myChart.Chart;

            chartRange = xlWorkSheet.get_Range("A6", "B" + (counter-1));



            chartPage.SetSourceData(chartRange, misValue);
            chartPage.ChartType = Excel.XlChartType.xlPie;// type of chart
            chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, Excel.XlDataLabelsType.xlDataLabelsShowLabel, true, false, false, true, false, true);// set label
            chartPage.Export(@"C:\excel_chart_export.bmp","BMP",misValue );


            #endregion    

#3


1  

The solution above didn't work for me in VS 2013 and Excel 2013.

上面的解决方案在VS 2013和Excel 2013中对我不起作用。

Add the following references:

添加以下参考:

Microsoft.Office.Core
Microsoft.Office.Interop.Excel

and use the following method:

并使用以下方法:

Chart oChart = ...    
oChart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementDataLabelOutSideEnd);

Source (MSDN):

来源(MSDN):

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.setelement.aspx https://msdn.microsoft.com/en-us/library/microsoft.office.core.msochartelementtype.aspx

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.setelement.aspx https://msdn.microsoft.com/en-us/library/microsoft.office.core .msochartelementtype.aspx

#1


6  

Try this (TRIED AND TESTED)

试试这个(经过试验和测试)

    private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
    {
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
        Excel.Chart chartPage = myChart.Chart;

        Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
        Excel.Series series1 = seriesCollection.NewSeries();
        series1.XValues = activeSheet.Range["E1", "E3"];
        series1.Values = activeSheet.Range["F1", "F3"];

        chartPage.ChartType = Excel.XlChartType.xlDoughnut;

        Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;

        series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
    }

One quick question though. If you are not using xRange and yRange then why declare it?

但是一个简单的问题。如果你没有使用xRange和yRange那么为什么要声明呢?

This is the completed code that is tried and tested.

这是经过试验和测试的完整代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Excel.Application();

            xlexcel.Visible = true;
            // Add a Workbook
            xlWorkBook = xlexcel.Workbooks.Add();

            // Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 5] = "Apples";
            xlWorkSheet.Cells[2, 5] = "Oranges";
            xlWorkSheet.Cells[3, 5] = "Pears";

            xlWorkSheet.Cells[1, 6] = "80";
            xlWorkSheet.Cells[2, 6] = "65";
            xlWorkSheet.Cells[3, 6] = "45";

            Excel.ChartObjects myCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);

            // Specified xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6] just for the heck of it.
            DrawFractionChart(xlWorkSheet, myCharts, xlWorkSheet.Cells[3, 6], xlWorkSheet.Cells[3, 6]);

            //Once done close and quit Excel
            //xlWorkBook.Close(true, misValue, misValue);
            //xlexcel.Quit();

            //releaseObject(xlWorkSheet);
            //releaseObject(xlWorkBook);
            //releaseObject(xlexcel);
        }

        private void DrawFractionChart(Excel.Worksheet activeSheet, Excel.ChartObjects xlCharts, Excel.Range xRange, Excel.Range yRange)
        {
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 500, 200, 100);
            Excel.Chart chartPage = myChart.Chart;

            Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
            Excel.Series series1 = seriesCollection.NewSeries();
            series1.XValues = activeSheet.Range["E1", "E3"];
            series1.Values = activeSheet.Range["F1", "F3"];

            chartPage.ChartType = Excel.XlChartType.xlDoughnut;

            Excel.Axis axis = chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Excel.Axis;

            series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, true, true, true, true);
        }

        //private void releaseObject(object obj)
        //{
        //    try
        //    {
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        //        obj = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        obj = null;
        //        MessageBox.Show("Unable to release the Object " + ex.ToString());
        //    }
        //    finally
        //    {
        //        GC.Collect();
        //    }
        //} 
    }
}

SNAPSHOT

快照

将数据标签添加到excel饼图

#2


2  

            #region Export Excel To Chart

            Excel.Range chartRange;

            Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(300, 50, 500, 500);
            Excel.Chart chartPage = myChart.Chart;

            chartRange = xlWorkSheet.get_Range("A6", "B" + (counter-1));



            chartPage.SetSourceData(chartRange, misValue);
            chartPage.ChartType = Excel.XlChartType.xlPie;// type of chart
            chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, Excel.XlDataLabelsType.xlDataLabelsShowLabel, true, false, false, true, false, true);// set label
            chartPage.Export(@"C:\excel_chart_export.bmp","BMP",misValue );


            #endregion    

#3


1  

The solution above didn't work for me in VS 2013 and Excel 2013.

上面的解决方案在VS 2013和Excel 2013中对我不起作用。

Add the following references:

添加以下参考:

Microsoft.Office.Core
Microsoft.Office.Interop.Excel

and use the following method:

并使用以下方法:

Chart oChart = ...    
oChart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementDataLabelOutSideEnd);

Source (MSDN):

来源(MSDN):

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.setelement.aspx https://msdn.microsoft.com/en-us/library/microsoft.office.core.msochartelementtype.aspx

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.setelement.aspx https://msdn.microsoft.com/en-us/library/microsoft.office.core .msochartelementtype.aspx