Excel charts can be created programmatically. This procedure uses the Microsoft.Office.Interop.Excel functionality with the C# language. Charts and graphs are created with the ChartObjects property.
This C# example program creates Chart objects in Excel interop.

This example uses several hard-coded constant values. The range of the data we want to chart is encoded in the topLeft and bottomRight constants. You can see we use the ChartObjects property and then Add a Chart. We set the range of the chart with SetSourceData. We use the XlChartType.xlLine enumerated constant and call ChartWizard().
Tip: Include the Microsoft.Office.Interop.Excel namespace in the console program by right-clicking on References and selecting Add Reference...
Program that creates Excel chart from data [C# .NET 4.0]
using Microsoft.Office.Interop.Excel;
class Program
{
const string fileName = "C:\\Book1.xlsx";
const string topLeft = "A1";
const string bottomRight = "A4";
const string graphTitle = "Graph Title";
const string xAxis = "Time";
const string yAxis = "Value";
static void Main()
{
// Open Excel and get first worksheet.
var application = new Application();
var workbook = application.Workbooks.Open(fileName);
var worksheet = workbook.Worksheets[1] as
Microsoft.Office.Interop.Excel.Worksheet;
// Add chart.
var charts = worksheet.ChartObjects() as
Microsoft.Office.Interop.Excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as
Microsoft.Office.Interop.Excel.ChartObject;
var chart = chartObject.Chart;
// Set chart range.
var range = worksheet.get_Range(topLeft, bottomRight);
chart.SetSourceData(range);
// Set chart properties.
chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
chart.ChartWizard(Source: range,
Title: graphTitle,
CategoryTitle: xAxis,
ValueTitle: yAxis);
// Save.
workbook.Save();
}
}
Output
See graph image at top.
Depends on the input range and data.More on ChartWizard. Let's dissect the usage of ChartWizard above. We use the named parameters and default parameters functionality in the .NET Framework 4.0. In older versions of the .NET Framework, you will need to specify the Missing value yourself. You can see examples of this on the other Excel article.
Excel Interop: Microsoft.Office.Interop.ExcelNote: Code contributed by Randall Kelsey was adapted for the example.
The program requires a Excel document named Book1.xlsx located at C:\Book1.xlsx. In this file, you need to add four values in the first column. The program, upon execution, will create a chart based on those four values. This is a simplistic way of making a chart, but it shows some of the basics.

We looked at a simple example of charts in the Microsoft.Office.Interop.Excel namespace. Many options can be changed to create different charts based on different data ranges. We also showed how named parameters and default parameters from the .NET Framework 4.0 can clean up Microsoft.Office.Interop.Excel code significantly.
File Handling