Insert a Chart in Excel Spreadsheet using EPPlus Library.

EPPlus is .NET Library that makes it very easy to manipulate Excel programmatically. It is based on Open XML.

https://epplus.codeplex.com/

Below is the sample code we can use to insert a chart in a excel spreadsheet.


public void UpdateExcelUsingEPPlus(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);

ExcelPackage p = new ExcelPackage(fileInfo);

// access the first sheet named Sheet1
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];

// specify cell values to be used for generating chart.
myWorksheet.Cells["C2"].Value = 10;
myWorksheet.Cells["C3"].Value = 40;
myWorksheet.Cells["C4"].Value = 30;

myWorksheet.Cells["B2"].Value = "Yes";
myWorksheet.Cells["B3"].Value = "No";
myWorksheet.Cells["B4"].Value = "NA";

// add chart of type Pie.
var myChart = myWorksheet.Drawings.AddChart("chart", eChartType.Pie);

// Define series for the chart
var series = myChart.Series.Add("C2: C4", "B2: B4");
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "My Chart";
myChart.SetSize(400, 400);

// Add to 6th row and to the 6th column
myChart.SetPosition(6, 0, 6, 0);

p.Save();

}

The output –

If we want to do it using Open XML SDK without using EPPlus Library we can refer to the below article.

https://msdn.microsoft.com/en-us/library/office/cc820055.aspx

Hope it helps..


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s