Last Updated:

How to programmatically insert image in Excel?

Sometimes there is a need to insert an image directly into an Excel document, unfortunately, it is in the document, at certain coordinates, and not in a specific cell, it is possible to insert such an object. Here are two possible ways to programmatically insert a picture.

Using early binding, i.e. using wrappers around the "insides" of Excel. To do this, we need to connect the same libraries as for reading data from Excel files.

using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
. . .
rg = m_workSheet.get_Range("A" + 1, "A" + 1);
float il, it, iw, ih;

// to bring the image dimensions to the dimensions used in the Shape
float zExcelPixel = 0.746835443f;
Image im = Image.FromFile(filePic);
// rg variable holding a reference to range relative to
// upper left corner of which the image should be inserted
// coordinate of the upper left corner where to insert - to be from range
il = (float)(double)rg.Left;// sizes come in double packed into object
it = (float)(double)rg.Top;

// image dimensions for Shape need to be converted
iw = zExcelPixel * im.Width;// get from original image width
ih = zExcelPixel * im.Height;

// Increase cell height
if (im.Height < 409)
rg.RowHeight = zExcelPixel * im.Height;
else
rg.RowHeight = 409;
                                                        
m_workSheet.Shapes.AddPicture(filePic, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, il, it, iw, ih);
. . .

Using late binding

...
// rg variable holding a reference to range relative to the upper left corner of which the image should be inserted
rg.Activate();//rg.Select();
object ob=worksSheet.GetType().InvokeMember("Pictures", BindingFlags.GetProperty, null, worksSheet, null);
ob.GetType().InvokeMember("Insert", BindingFlags.InvokeMethod, null, ob, new object[]{pathToPicFile});
...

Here's the entire code snippet inserting the image into an Excel document:

void InsertPicToExcel()
{
String filePic = @”d:\test.jpg”;
Excel._Application m_app = null;
 // Excel workbook.
 Excel.Workbook m_workBook = null;
// Excel page.
Excel.Worksheet m_workSheet = null;
// Range of cells
Excel.Range rg = null;

try
  {
      // Create an Excel application.
      m_app = new Excel.ApplicationClass();

      // The application is "invisible".
      m_app.Visible = false;
      // The application is controlled by the user.
      m_app.UserControl = true;

      // Adding an Excel workbook.
      // m_workBook = m_app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

      // Opening the Excel file.
      m_workBook = m_app.Workbooks.Open(@"D:\test.xlsx", Type.Missing, Type.Missing,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

      // Link to Excel page.
      m_workSheet = m_app.ActiveSheet as Excel.Worksheet;

  #region Insert pictures

      rg = m_workSheet.get_Range("A" + 1, "A" + 1);

      float il, it, iw, ih;
      float zExcelPixel = 0.746835443f;// to resize the image to the size used in the Shape

      Image im = Image.FromFile(filePic);
      // rg variable holding a reference to range relative to the upper left corner of which the image should be inserted
      // coordinate of the upper left corner where to insert - to be from range
      il = (float)(double)rg.Left;// sizes come in double packed into object
      it = (float)(double)rg.Top;

      // image dimensions for Shape need to be converted
      iw = zExcelPixel * im.Width;// get from original image width
      ih = zExcelPixel * im.Height;

    // Increase cell height
     if (im.Height < 409)
         rg.RowHeight = zExcelPixel * im.Height;
     else
         rg.RowHeight = 409;
                
     m_workSheet.Shapes.AddPicture(filePic, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, il, it, iw, ih);

     #endregion
}
  finally
  {
    // Save the file with the inserted image
      m_workBook.SaveCopyAs(@"d:\testPic.xlsx");

      // Close the book.
      m_workBook.Close(false, "", null);
      // Close the Excel application.
      m_app.Quit();
      m_workBook = null;
      m_workSheet = null;
      m_app = null;
      GC.Collect();
  }
}