Insert image into excel cells using c #

I am creating an excel sheet from datamable dyanamically. I can add text to different cells as needed. But I don't know how to add an image to the 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 a picture ..

I try like

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

      

Now how can I add / copy my imgg to this range? eg.

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

      

+3


source to share


3 answers


You can use 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();
            }
        } 

    }
}

      

+4


source


Have you tried giving a 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

      

0


source


You can easily add an image to an Excel spreadsheet (this assumes you are using an assembly reference Microsoft.Office.Interop.Excel 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 hardcoded ints if you want to fly face to face with Steve McConnell's advice in Code Complete about approving all numbers except sometimes 0 and 1

The image must be assigned to _platypusLogo. If you are using a C # utility application to create an Excel spreadsheet dynamically, you can add a PictureBox control to the form and then assign an image to it using its Image property (the control name, by default, pictureBox1) and then assign it to the spreadsheet in the following way:

_platypusLogo = pictureBox1.Image;

      

You can of course assign _platypusLogo directly / exclusively in code if you like.

0


source







All Articles