2017-04-04 52 views
0

我不能設法改變我的excel圖表在C#中的風格。c#圖表excel風格

我有這樣的: default design

,我想獲得這個: design I try to have

我讀了這個問題很多話題,嘗試過的很多,似乎沒有任何工作.. 。 我設法得到我想要爲我的餅圖,但它不工作對我的「列」 Chart:

我嘗試這樣做:

object paramChartFormat = 1; //tried to change it to 201, 209, ... not working :(

然後這個(在excel中找到了一個名稱與宏,因爲我看到它是獲得我們希望在另一個問題上設計的名稱的方式在這裏在stackoverflow),但設計仍然是藍色列「標準」 :

Microsoft.Office.Interop.Excel.XlChartType.xlColumnStacked; 

這裏是我的代碼:

 object paramChartFormat = 1; 
     object paramCategoryLabels = 1; 
     object paramSeriesLabels = 1; 
     //bool paramHasLegend = true; 
     object paramTitle = "Collection"; 
     object paramCategoryTitle = ""; 
     object paramValueTitle = ""; 
     Range dataRange = null; 
     ChartObjects chartObjects = null; 
     //ChartObject newChartObject = null; 
     object paramMissing = Type.Missing; 
     string tuple = "C" + Row.ToString(); 
     dataRange = sheet.get_Range("A1", tuple); 
     chartObjects = (ChartObjects)(sheet.ChartObjects(paramMissing)); 
     /*newChartObject = chartObjects.Add(0, 100, 300, 300); 
     newChartObject.Chart.ChartWizard(dataRange, XlChartType.xl3DColumn, paramChartFormat, XlRowCol.xlRows, paramCategoryLabels, paramSeriesLabels, paramHasLegend, paramTitle, paramCategoryTitle, paramValueTitle, paramMissing); 
     */ 
     //Number of copies 
     Microsoft.Office.Interop.Excel.ChartObject chartObjectnbex = sheet.ChartObjects().Add((float)sheet.get_Range("B1").Left, (float)sheet.get_Range("B"+(Row+2).ToString()).Top, 300, 300); 
     chartObjectnbex.Chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnStacked; 
     Microsoft.Office.Interop.Excel.Series seriesnbex = chartObjectnbex.Chart.SeriesCollection().Add(sheet.Range["B2:B" + Row.ToString()]); 
     seriesnbex.XValues = sheet.Range["A2:A" + Row.ToString()]; 
     seriesnbex.Name = "Nombre d'Exemplaires"; 
     //Nbre documents différents 

//此代碼(餅圖工作):

Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)chartObjects.Add((float)sheet.get_Range("B1").Left, (float)sheet.get_Range("B" + (Row + 77).ToString()).Top, 300, 300); 
      Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart; 

      Microsoft.Office.Interop.Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection(); 
      Microsoft.Office.Interop.Excel.Series series1 = seriesCollection.NewSeries(); 
      series1.XValues = sheet.Range["A2", "A"+Row.ToString()]; 
      series1.Values = sheet.Range["B2", "B" + Row.ToString()]; 
      chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlDoughnut; 
      chartPage.HasTitle = true; 
      chartPage.ChartTitle.Text = "Nombre d'exemplaires"; 
      Microsoft.Office.Interop.Excel.Axis axis = chartPage.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Microsoft.Office.Interop.Excel.Axis; 

      series1.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, false, false, false, true); 

你知道我在做錯什麼來改變設計嗎?

非常感謝,

R.

回答

0

我發現我在做什麼錯。我只有一個系列,所以它把我所有的數字都視爲一個組。我寫了這個代碼就萬事大吉了,它的工作:

   //Nbre documents différents 
      Microsoft.Office.Interop.Excel.ChartObject chartObjectnbdd = sheet.ChartObjects().Add((float)sheet.get_Range("E1").Left, (float)sheet.get_Range("B" + (Row + 2).ToString()).Top, 300, 300); 
      chartObjectnbex.Chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlBarClustered; 
      chartObjectnbdd.Chart.HasTitle = true; 
      chartObjectnbdd.Chart.ChartTitle.Text = "Nombre de documents différents"; 
      Microsoft.Office.Interop.Excel.SeriesCollection seriescollnbdd = chartObjectnbdd.Chart.SeriesCollection(); 
      Microsoft.Office.Interop.Excel.Series seriesnbdd = null; 
      i = 2; 
      while (i <= Row) 
      { 
       seriesnbdd = seriescollnbdd.NewSeries(); 
       seriesnbdd.Name = excel.Cells[i, 1].Value.ToString(); 
       seriesnbdd.Values = sheet.Range["C" + i.ToString() + ":C" + i.ToString()]; 
       i++; 
      } 

輸出(唯一現在是「1」,我想不出如何刪除,但它的確定):

Chart with colors

謝謝大家!