为什么我的Excel图表会生成与饼图不对应的图例?

时间:2016-03-21 15:26:56

标签: c# excel-interop excel-charts

我正在使用以下代码生成基本的Excel图表:

object misValue = System.Reflection.Missing.Value;
//add data 
_xlSheet.Cells[11, 11] = "PhooBar";
_xlSheet.Cells[11, 12] = "Student1";
_xlSheet.Cells[11, 13] = "Student2";
_xlSheet.Cells[11, 14] = "Student3";

_xlSheet.Cells[12, 11] = "Term1";
_xlSheet.Cells[12, 12] = "80";
_xlSheet.Cells[12, 13] = "65";
_xlSheet.Cells[12, 14] = "45";

_xlSheet.Cells[13, 11] = "Term2";
_xlSheet.Cells[13, 12] = "78";
_xlSheet.Cells[13, 13] = "72";
_xlSheet.Cells[13, 14] = "60";

_xlSheet.Cells[14, 11] = "Term3";
_xlSheet.Cells[14, 12] = "82";
_xlSheet.Cells[14, 13] = "80";
_xlSheet.Cells[14, 14] = "65";

_xlSheet.Cells[15, 11] = "Term4";
_xlSheet.Cells[15, 12] = "75";
_xlSheet.Cells[15, 13] = "82";
_xlSheet.Cells[15, 14] = "68";

Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)_xlSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(468, 160, 348, 268);
Excel.Chart chartPage = myChart.Chart;

chartRange = _xlSheet.get_Range("K11", "O15"); // K == 11, O == 15
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlPieExploded;

我对控制标签展示位置here有疑问。

现在我想知道为什么传奇中有四个饼图而且有五个项目:

enter image description here

ISTM图表引擎的图例应与馅饼匹配。这是什么交易?

1 个答案:

答案 0 :(得分:0)

通过将代码更改为此,饼图和图例对应:

object misValue = System.Reflection.Missing.Value;
//add data 
_xlSheet.Cells[12, 11] = "Term1";
_xlSheet.Cells[12, 12] = "80";
_xlSheet.Cells[12, 13] = "65";
_xlSheet.Cells[12, 14] = "45";

_xlSheet.Cells[13, 11] = "Term2";
_xlSheet.Cells[13, 12] = "78";
_xlSheet.Cells[13, 13] = "72";
_xlSheet.Cells[13, 14] = "60";

_xlSheet.Cells[14, 11] = "Term3";
_xlSheet.Cells[14, 12] = "82";
_xlSheet.Cells[14, 13] = "80";
_xlSheet.Cells[14, 14] = "65";

_xlSheet.Cells[15, 11] = "Term4";
_xlSheet.Cells[15, 12] = "75";
_xlSheet.Cells[15, 13] = "82";
_xlSheet.Cells[15, 14] = "68";

Excel.Range chartRange;

//return;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)_xlSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(468, 160, 348, 268);
Excel.Chart chartPage = myChart.Chart;

chartRange = _xlSheet.Range[_xlSheet.Cells[12, 11], _xlSheet.Cells[15, 14]];

我从“get_Range”变为更高效,更容易理解的“Range []”,现在饼图和图例之间存在1:1的对应关系:

enter image description here