Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

How to Add an Image to an Excel Sheet

4.00/5 (2 votes)
14 Nov 2015CPOL 7.4K  
Adding an image to an Excel spreadsheet

Adding Images Spo-Dee-O-Dee

You can add an image to your Excel spreadsheet quite easily (this assumes that you are using Microsoft.Office.Interop.Excel assembly reference, in C#) like this:

C#
private Worksheet _xlSheet;
private Image _platypusLogo;
. . .
private void AddImage()
{
    Clipboard.SetDataObject(_platypusLogo, true);
    var cellRngImg = (Range)_xlSheet.Cells[IMAGE_ROW, IMAGE_COLUMN];
    _xlSheet.Paste(cellRngImg, _platypusLogo);
}

Note that "IMAGE_ROW" and "IMAGE_COLUMN" are int constants or you can just use hard-coded ints, if you want to fly in the face of Steve McConnell's advice in Code Complete about constantifying all numbers other than sometimes 0 and 1.

An image needs to be assigned to _platypusLogo. If you are using a C# utility app to dynamically generate the Excel spreadsheet, you could add a PictureBox control to a form, and then assign an image to it via its Image property (the control is named, by default, pictureBox1), and then assign it to the spreadsheet this way:

C#
_platypusLogo = pictureBox1.Image;

Of course, you can assign to _platypusLogo directly/exclusively in code, too, if you prefer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)