2016-03-21 51 views
0

我生成一個基本的Excel圖表與此代碼:爲什麼我的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該圖表引擎的傳奇應該匹配的餡餅。這是怎麼回事?

+0

因爲你的傳奇4項,紫色的將是「」一個.. – BugFinder

+1

難道不是因爲這樣的:1:派件和傳說之間的1相契合? '_xlSheet.Cells [11,11] =「」;' – Ian

+1

'_xlSheet.Cells [11,11] =「」;'改爲說'foo',你會在你的傳說中得到'foo' ... –

回答

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」更改爲更高效,更易於神交「範圍。[]」和現在有

enter image description here

+0

嘿,你解決了這個問題。大! ;) – Ian