How to Insert Image in Excel Worksheet with C#

Although Excel is used to save, display and calculate data, sometimes, people may add external contents in Excel files, for example, image.

Generally speaking, if we want to insert an image in Excel file, the image may be related to data information. For example, if an Excel file saves some data information of countries, people may insert country flag image in relevant data row.

In this post, I want to introduce a method about how to insert image in Excel by using C#. I will create a new Excel file and then insert an image from my computer to the current worksheet.

Also, Spire.XLS, component for operating Excel, is used in this example, so I add its dll file as reference in my project.

Steps:

  1. Declare a new workbook and add an empty worksheet in this workbook.Then, initialize worksheet.
  2. Add picture in worksheet by using sheet.Pictures.Add() method. There are three parameter passed in this method, start row, start column and image. 
  3. Save this file with a file name and launch it.

Use the following code.

using System.Drawing;
using Spire.Xls;

namespace WriteImage
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create Workbook and Initialize Worskheet
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            //Define Location and Insert Picture
            sheet.Pictures.Add(1,1, @”D:\work\My Documents\VIP.bmp”);

            //Save and Launch File
            workbook.SaveToFile(“WriteImage.xls”);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Result:

_________________________________________________________________________________

Click Here to LEARN MORE about Spire.XLS

Click Here to DOWNLOAD Spire.XLS

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.

2 thoughts on “How to Insert Image in Excel Worksheet with C#”

  1. Hello there, I do believe your blog might be having internet browser compatibility problems.
    Whenever I take a look at your blog in Safari, it looks fine but when opening in I.E., it has some overlapping issues.

    I simply wanted to provide you with a quick heads up!
    Besides that, wonderful site!

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