2017-11-26 45 views
0

私はExcelチャートを持っています。凡例の値を追加するにはどうすればよいですか?凡例をグラフに追加する

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); 
    Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); 

    Excel.Chart chartPage = myChart.Chart; 
    chartRange = xlWorkSheet.get_Range("B12", "B15"); 
    chartPage.SetSourceData(chartRange, misValue); 
    chartPage.ChartType = Excel.XlChartType.xl3DPieExploded; 
    foreach (Series series in chartPage.SeriesCollection()) 
    { 
     series.Name = "Diagram Name"; 
    } 

    chartPage.HasLegend = true; 
    chartPage.Is 

    //export chart as picture file 
    String destPath = Directory.GetCurrentDirectory(); 
    chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue); 

メソッド "シリーズ"は利用できません。

+1

あなたが取得している最後のテキスト行のエラーメッセージですか? – QHarr

+0

エラーはありません。私は伝説の図が必要です。 – randomUser

答えて

0

これは私のために働いた。いくつかのデータを追加したので、この例は単独で機能します。

Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 

Workbook xlWorkBook; 
Worksheet xlWorkSheet; 
object misValue = System.Reflection.Missing.Value; 
xlWorkBook = xlApp.Workbooks.Add(misValue); 
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); 
xlWorkSheet.Name = "Sheet"; 

ChartObjects xlCharts = (ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); 
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250); 

Chart chartPage = myChart.Chart; 

chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom; 
chartPage.HasTitle = true; 
chartPage.ChartTitle.Text = "Title"; 

xlWorkSheet.Cells[12, 1] = "Test"; 
xlWorkSheet.Cells[13, 1] = "Test2"; 
xlWorkSheet.Cells[14, 1] = "Test3"; 
xlWorkSheet.Cells[15, 1] = "Test4"; 
xlWorkSheet.Cells[12, 2] = 12; 
xlWorkSheet.Cells[13, 2] = 13; 
xlWorkSheet.Cells[14, 2] = 14; 
xlWorkSheet.Cells[15, 2] = 15; 



var sc = chartPage.SeriesCollection(); 
var series1 = sc.NewSeries(); 
series1.Name = "Title"; 
series1.XValues = $"'{xlWorkSheet.Name}'!A12:A15"; 
series1.Values = $"'{xlWorkSheet.Name}'!B12:B15"; 
series1.ChartType = XlChartType.xl3DPieExploded; 
chartPage.ApplyDataLabels(); 

//export chart as picture file 
String destPath = Directory.GetCurrentDirectory(); 
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue); 
0
[![Excel.Range chartRange; 


      Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); 
      Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); 
      Excel.Chart chartPage = myChart.Chart; 
      //диаграмма 2 
      chartRange = xlWorkSheet.get_Range("D12", "E16"); 
      chartPage.SetSourceData(chartRange, misValue); 
      chartPage.ChartType = Excel.XlChartType.xl3DPieExploded; 
      foreach (Series series in chartPage.SeriesCollection()) 
      { 
       series.Name = "Уровень удовлетворенности респондентов длительностью ожидания"; 
      } 
      chartPage.HasLegend = true; 

      //export chart as picture file 
      String destPath = Directory.GetCurrentDirectory(); 
      chartPage.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, false, true, false, false, false, false, true, false, false); 
      chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue); 
      //конец диаграмы 2][1]][1] 
関連する問題