user3452210
user3452210

Reputation: 147

Insert an image into excel cells using c#

I am Generating a Excel sheet from datatable dyanamically. I am able to add text into different cells as i needed. But i do not have any idea about how to add picture into a specified range..

        Excel.Application oApp = new Excel.Application();
        oApp.Application.Workbooks.Add(Type.Missing);

        oApp.Range["B2", "C4"].Merge(Type.Missing);

Here i want to add picture..

I am trying like

        System.Drawing.Image imgg = System.Drawing.Image.FromFile("c:\\D.jpg");

Now how could i add/copy my imgg into this range?? e.g.

App.Range["B2", "C4"]

Upvotes: 3

Views: 20176

Answers (3)

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:

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:

_platypusLogo = pictureBox1.Image;

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

Upvotes: 1

M.S
M.S

Reputation: 298

Have you tried giving position with excelpicture?

var picture = worksheet.Drawings.AddPicture("image_name", imgg);
picture.SetPosition(int row,int rowoffsetpixels,int column,int coloumoffsetpixels);//position for the image

Upvotes: 0

Mohit
Mohit

Reputation: 11324

You can get benefited with the following

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            //add some text 
            xlWorkSheet.Cells[1, 1] = "Text1";
            xlWorkSheet.Cells[2, 1] = "Text2";

            xlWorkSheet.Shapes.AddPicture("C:\\sample.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45); 


            xlWorkBook.SaveAs("MyExcelFile.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);

            MessageBox.Show ("File created !");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 

    }
}

Upvotes: 4

Related Questions