Inserting A Picture To Excel Document Using C#

The following code can be used to insert an image into Excel document using C#.


        using System;
        using System.Windows.Forms;
        using Microsoft.Office.Interop.Excel;
        using System.Reflection;
        using System.Runtime.InteropServices;

        void button1_Click(object sender, EventArgs e)
        {

            ApplicationClass objExcel = null;
            Workbooks objBooks = null;
            _Workbook objBook = null;
            Sheets objSheets = null;
            _Worksheet objSheet = null;
            Range objRange = null;
            int row = 1, col = 1;
            try
            {

                objExcel = new ApplicationClass();
                objBooks = objExcel.Workbooks;
                objBook = objBooks.Add(XlWBATemplate.xlWBATWorksheet);
                objSheets = objBook.Worksheets;
                objSheet = (_Worksheet)objSheets.get_Item(1);
                objRange = (Range)objSheet.Cells[row, col];
                //Insert picture into Excel Shee
                Picture picture = ((Pictures)objSheet.Pictures(Type.Missing)).Insert(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg", Type.Missing);

                //Save Excel document
                objSheet.Name = "Sample Sheet";
                object objOpt = Missing.Value;
                objBook.SaveAs(@"C:\TestSheet1.xls", objOpt, objOpt, objOpt, objOpt, objOpt, XlSaveAsAccessMode.xlNoChange, objOpt, objOpt, objOpt, objOpt, objOpt);
                objBook.Close(false, objOpt, objOpt);

            }
            catch
            {
                MessageBox.Show("Error Inserting Picture in Excel Document");
            }
            finally
            {
                objExcel = null;
                objBooks = null;
                objBook = null;
                objSheets = null;
                objSheet = null;
                objRange = null;
                ReleaseComObject(objExcel);
                ReleaseComObject(objBooks);
                ReleaseComObject(objBook);
                ReleaseComObject(objSheets);
                ReleaseComObject(objSheet);
                ReleaseComObject(objRange);
            }
        }
        //Release COM objects from memory
        public void ReleaseComObject(object reference)
        {
            try
            {
                while (System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) <= 0)
                {
                }
            }
            catch
            {
            }
        }

Advertisements

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


%d bloggers like this: