How to Convert Excel Chart to Image in C#, VB.NET

Introduction

Charts are visually appealing and make it easy for users to see comparisons, patterns, and trends in data. Sometimes, you do need to present the chart in your applications or web pages. Simply you want the chart should be rendered as an image, so that you may paste it into your applications with ease. In this post, I’ll introduce how to extract every chart in Excel sheet and render as image using .NET Excel library in C#/Vb.NET.

Method

  •  Download Spire.XLS, and Spire.Xls.dll as reference in your project.
  •  Load the test file that contains two charts and get the worksheet.
  •  Initialize a new instance of image array to store the Bitmap images which are converted from charts.
  •  Traverse every item in image array and save them to specified image format.

Entire Code

[C#]

Workbook workbook = new Workbook();

 workbook.LoadFromFile(“sample.xlsx”, ExcelVersion.Version2010);

 Worksheet sheet=workbook.Worksheets[0];

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

 for (int i = 0; i < imgs.Length; i++)

 {

     imgs[i].Save(string.Format(“img-{0}.png”, i), ImageFormat.Png);

 }

[VB.NET]

Dim workbook As New Workbook()

workbook.LoadFromFile(“chart.xlsx”, ExcelVersion.Version2010)

Dim sheet As Worksheet = workbook.Worksheets(0)

Dim imgs As Image() = workbook.SaveChartAsImage(sheet)

For i As Integer = 0 To imgs.Length – 1

    imgs(i).Save(String.Format(“img-{0}.png”, i), ImageFormat.Png)

Next

Result

charts

b-chart

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

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