2017-03-16 3 views
0

私はinterop Excelを使用してExcelファイルにグラフを作成します。シリーズチャート(Interop Excel)の値を設定

私はグラフを作成し、最初の時間のシリーズの値を設定するとき、私は問題はありません。

これは、私がエラーが2回目にするときです。

私は、問題を再現するためにPOCを作成しました:

using System; 
using System.Linq; 
using System.Runtime.InteropServices; 
using Excel = Microsoft.Office.Interop.Excel; 

namespace POC_Excel 
{ 
    static class Program 
    { 
     static void Main(string[] args) 
     { 
      Excel.Application excel = null; 
      try 
      { 
       excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Please run Excel"); 
       Console.ReadLine(); 
       Environment.Exit(0); 
      } 

      try 
      { 
       LockUnlock(excel, true); 

       var shapes = excel.ActiveSheet.Shapes; 
       Console.WriteLine(" --- Create chart --- "); 
       Excel.Shape shape = shapes.AddChart2(-1, Excel.XlChartType.xlColumnClustered); 

       Console.WriteLine(" --- 1st chart filling --- "); 
       CreateSeries(shape); 

       Console.WriteLine(" --- 2nde chart filling --- "); 
       CreateSeries(shape); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error : " + ex); 
      } 
      finally 
      { 
       LockUnlock(excel, false); 
       Console.ReadLine(); 
      } 
     } 

     static void CreateSeries(Excel.Shape shape) 
     { 
      var seriesCollection = (Excel.SeriesCollection)shape.Chart.SeriesCollection(); 
      while (0 < seriesCollection.Count) 
       seriesCollection.Item(1).Delete(); 

      var donnee1 = new object[] { 10, 20, 30 }; 
      var series = seriesCollection.NewSeries(); 
      Console.WriteLine("Add values to serie : " + donnee1.Select(v => v.ToString()).Aggregate((a, b) => a + ", " + b)); 
      series.Values = donnee1; 
      Console.WriteLine("Serie data : " + series.Formula); 

      series.ChartType = Excel.XlChartType.xlDoughnut; 

      for (int i = 0; i < donnee1.Length; i++) 
      { 
       Excel.Point pt = series.Points(i + 1); 
       Console.WriteLine("points : " + pt.Name); 
      } 
     } 

     private static void LockUnlock(Excel.Application excel, bool isFiger) 
     { 
      excel.DisplayAlerts = !isFiger; 
      excel.ScreenUpdating = !isFiger; 
      excel.EnableEvents = !isFiger; 
      excel.Interactive = !isFiger; 
     } 
    } 
} 

あなたは私を助けてもらえますか?私は1つの形状に2つのチャートを作成する機能を見たことがない

おかげ

+0

「2回目にエラーが発生したときです。」何のエラー? COM例外? C#の例外?どのコード行が実際に破られていますか? – Drakestar

答えて

0

。私はそれが失敗していると思うだろう。試してみてください...

  Excel.Shape shape; 

      shape = shapes.AddChart2(-1, Excel.XlChartType.xlColumnClustered); 
      Console.WriteLine(" --- 1st chart filling --- "); 
      CreateSeries(shape); 

      shape = shapes.AddChart2(-1, Excel.XlChartType.xlColumnClustered); 
      Console.WriteLine(" --- 2nde chart filling --- "); 
      CreateSeries(shape); 
+0

私は問題を再現するためにPOCでそれを行います。 私のソフトウェアのdifferentsデータで2種類のアップデートに対応しています。 – Maverick

関連する問題