Insert a Chart in Excel Spreadsheet using EPPlus Library.
Views (4940)
EPPlus is .NET Library that makes it very easy to manipulate Excel programmatically. It is based on Open XML.
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..
Filed under: C#, EPPlus Tagged: C#

This was originally posted here.
*This post is locked for comments