本文介绍了将数据标签添加到excel饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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轴= chartPage.Axes(Excel.XlAxisType.xlValue,Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary)为Excel.Axis;
}
我不知道如何打开数据标签。我到处都是googled,但是迄今为止,还没有任何帮助。
解决方案
尝试这个( 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轴= chartPage.Axes(Excel.XlAxisType.xlValue,Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary)为Excel.Axis;
series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent,true,true,false,true,true,true,true);
}
一个快速的问题。如果您不使用 xRange
和 yRange
那么为什么要声明?
这是经过测试的完整代码。
using System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用Excel = Microsoft.Office.Interop.Excel;
命名空间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;
//添加一个工作簿
xlWorkBook = xlexcel.Workbooks.Add();
//将Sheet 1作为要使用的工作表
xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells [1,5] =苹果;
xlWorkSheet.Cells [2,5] =Oranges;
xlWorkSheet.Cells [3,5] =梨;
xlWorkSheet.Cells [1,6] =80;
xlWorkSheet.Cells [2,6] =65;
xlWorkSheet.Cells [3,6] =45;
Excel.ChartObjects myCharts =(Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
//指定的xlWorkSheet.Cells [3,6],xlWorkSheet.Cells [3,6]只是为了它。
DrawFractionChart(xlWorkSheet,myCharts,xlWorkSheet.Cells [3,6],xlWorkSheet.Cells [3,6]);
//完成关闭并退出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轴= chartPage.Axes(Excel.XlAxisType.xlValue,Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary)为Excel.Axis;
series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent,true,true,false,true,true,true,true);
}
// private void releaseObject(object obj)
// {
// try
// {
//系统.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
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.
解决方案
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?
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饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!