Create Excel Chart and Export Chart as Image in C#


This article presents how to generate the Excel chart from data and then save the chart as Image programmatically using EEPlus and Spire.XLS in C#. With free libraries, you can save a lot time to write your own code.

Background


When we need to read and write Excel in C#, at first we think about NPOI, but it is not supported to work with chart. Then I found EPPlus, it is very convenient to generate the chart from the data. To my surprise, it is user-friendly and easy to use. I don't want to share the whole Excel file to others except the chart, so I need to save the chart as pintable documents. For EPPlus doesn't support it, I finally found Free Spire.XLS to finish the job. This article will show you how to generate the Excel chart and then save the chart to Image in C#.

Application Overview


In this project, firstly I need to export the data and create a column chart by EPPlus. Then use Free Spire.XLS to export the chart as .PNG image format.

Brief Introduction of EPPlus and Spire.XLS:

EPPlus (http://epplus.codeplex.com) using the Open Office Xml format (xlsx) and it is a .NET library to read and write Excel 2007/2010 files.
Free Spire.XLS (http://freenetexcel.codeplex.com) is a free Excel API with the key feature of conversion; it supports to export Excel into PDF, Image, XPS, XML and HTML, etc.

Steps of Creating Excel Chart:


It is quite easy to create chart via EPPlus and it supports multiple types of charts, such as pie chart, bar chart, line chart, etc. This sample will show you how to create column chart in three steps.

Step 1: Create the chart and we can set the type via eChartType.

ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);

Step 2: Choose the data for the chart. We can use chart.Series.Add(Y axis, X axis) to set the data for chart.

ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);
serie.HeaderAddress = worksheet.Cells[1, 3];


Step 3: Set the format for the chart.

chart.SetPosition(150, 10);
chart.SetSize(500, 300);
chart.Title.Text = "Sales Trend";
chart.Title.Font.Color = Color.FromArgb(89, 89, 89);
chart.Title.Font.Size = 15;
chart.Title.Font.Bold = true;
chart.Style = eChartStyle.Style15;
chart.Legend.Border.LineStyle = eLineStyle.Solid;
chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);


The full codes of my project will be available at the end of this article, now view the effective screenshot of the creating chart by using EPPlus:

ExcelChart

Export Chart as Image:


Now we need to use free Spire.XLS to save Excel chart into Image.

Step 1: Create a new workbook and load the file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx", ExcelVersion.Version2010);


Step 2: Get the worksheet that contains the chart from workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Initialize a new instance of image array to store the Bitmap images which are converted from charts.

Image[] imgs = workbook.SaveChartAsImage(sheet);

Step 4: Traverse every item in image array and save them to specified image format.

for (int i = 0; i < imgs.Length; i++)
{
imgs[i].Save(string.Format("img-{0}.png", i), ImageFormat.Png);
}


Check the result image file as below:

ChartToImage


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: