title: Creating multiple Excel chart objects using c#
body: <p>I've spent hours now trying to create a few charts in Excel from my C# application. I'm trying to create more than one chart object. Is there a better way of doing this? I'm sure the line "chartObject[col] = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);" is where I'm going wrong. </p>
  
  <p>At the moment, when I call this line, it sometimes creates a a copy of the last chart, but sometimes it works. I can't understand the logic to it at all.</p>
  
  <p>Thanks</p>
  
  <pre><code>private void CreateCharts(Excel.Worksheet oWS, int numRows, int numCols)
      {
          Excel.Workbook oWB = (Excel.Workbook)oWS.Parent;
          Excel.Series oSeries;
          //Excel._Chart chartObject;
          Excel.Chart[] chartObject = new Excel.Chart[numCols];
          Excel.SeriesCollection[] oSeriesCollection = new Excel.SeriesCollection[numCols];
          int length = numRows + 2;
          string colname;
  
          //then you can assign as much as series you want,
          for (int col = 0; col &lt; numCols; col++)
          {
              //create a new chart
              chartObject[col] = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
              chartObject[col].ChartType = Excel.XlChartType.xlLine;
              oSeriesCollection[col] = (Excel.SeriesCollection)chartObject[col].SeriesCollection();
  
              //add the actual occupancy
              colname = GetExcelColumnName(col * 3 + 1);
              oSeries = oSeriesCollection[col].NewSeries();
              oSeries.Values = oWS.Range[colname + "2", colname + length];
  
              //add the expected occupancy
              colname = GetExcelColumnName(col * 3 + 2);
              oSeries = oSeriesCollection[col].NewSeries();
              oSeries.Values = oWS.Range[colname + "2", colname + length];
          }
      }
  </code></pre>