4

私はいくつかのデータで円グラフを描いています:

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;
}

データラベルをオンにする方法がわかりません。私はそれをどこでもグーグルで検索しましたが、悲しいことにこれまでのところ何も役に立ちませんでした。

4

3 に答える 3

6

これを試してください(試行およびテスト済み

    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);
    }

簡単な質問ですが。xRange使用していないのにyRange、なぜそれを宣言するのですか?

これは、試行およびテストされた完成したコードです。

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();
        //    }
        //} 
    }
}

スナップショット

ここに画像の説明を入力

于 2012-08-07T04:48:41.527 に答える
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    
于 2015-07-13T08:39:28.950 に答える
1

上記の解決策は、VS 2013 および Excel 2013 では機能しませんでした。

次の参照を追加します。

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

次の方法を使用します。

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

ソース (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

于 2016-07-13T17:36:42.527 に答える