C# .NET - How to Insert a picture into excel using c#

Asked By song yifeng on 13-Jan-04 08:32 PM
How to Insert a picture into excel worksheet using c#?
i am using Microsoft Excel 11.0 Object Library.
thanks!

How to Insert a picture into excel using c# - Asked By subbulakshmi vairavan on 06-Apr-04 07:07 AM

Try this code for inserting  a picture in excel. This would help you

private void insertImage()
		{
			Excel.Application ThisApplication = new Excel.ApplicationClass();
			Excel.Workbook ThisWorkBook;


			string _stFileName = "D:\\Test.xls";
			string _stPicture = "D:\\LOGO.bmp";
			object missing = System.Reflection.Missing.Value;
			
			try
			{
				ThisWorkBook = ThisApplication.Workbooks.Open(_stFileName,missing,missing,5,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
				Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];
			
				ThisSheet.Cells[1,1] = "test";
			
				Excel.Range oRange = (Excel.Range)ThisSheet.Cells[1,2];

				Image oImage = Image.FromFile(_stPicture);
				oRange.set_Item(1,1,oImage);
				ThisSheet.Paste(oRange,_stPicture);
				ThisWorkBook.Save();
			}
			catch(Exception oEx)
			{
			}
			finally
			{
				ThisApplication.Quit();
				ThisWorkBook = null;
				ThisApplication = null;
			}

		}

Here is the solution - Asked By subbulakshmi vairavan on 06-Apr-04 07:53 AM

private void insertImage()
{
            Excel.Application ThisApplication = new Excel.ApplicationClass();
            Excel.Workbook ThisWorkBook;


	string _stFileName = "D:\\Test.xls";
	string _stLOGO = "D:\\LOGO.bmp";
		
	object missing = System.Reflection.Missing.Value;
	
	try
	{
	ThisWorkBook = ThisApplication.Workbooks.Open(_stFileName,missing,missing,5,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);

	Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];
			
				
	Excel.Range oRange = (Excel.Range)ThisSheet.Cells[10,1];
		
				
	Image oImage = Image.FromFile(_stLOGO);
	System.Windows.Forms.Clipboard.SetDataObject(oImage,true);
	ThisSheet.Paste(oRange,_stLOGO);
	


	ThisWorkBook.Save();
	}
	catch(Exception oEx)
	{		}
	finally
	{
		
		ThisApplication.Quit();
		ThisWorkBook = null;
		ThisApplication = null;
	}

}

Another way to insert a picture in Excel - Asked By Jeff Ruys on 14-Dec-04 11:31 PM

Another way to insert a picture in Excel is to use the Worksheet.Shapes.AddPicture method.
After opening Excel and creating a workbook object and worksheet object (such as CurSheet) you can use the following code to insert a picture, size, and position it.

  string path = "C:\\TestBook.xls";
  string picName = "C:\\SomePic.jpg";
			
  Excel.Application app = new Excel.ApplicationClass();
  Excel.Workbook WB = app.Workbooks.Open(path, 0, false, 5,"", "", false,   
        Excel.XlPlatform.xlWindows, "", true, false,0, true, false, false);
  Excel.Sheets WS = WB.Worksheets;
  Excel.Worksheet CurSheet = (Excel.Worksheet)WS.get_Item("Sheet1");
  Excel.Range CurCell = (Excel.Range)CurSheet.get_Range("A1", "A1");
  app.Visible = true;
			
  CurSheet.Shapes.AddPicture(picName, Microsoft.Office.Core.MsoTriState.msoFalse,  
        Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 100, 100);

  The documentation on this method is located at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl11/html/xlmthAddPicture1.asp
insert picture in excel - bolton ss replied to song yifeng on 14-Jul-08 06:44 AM

here is the code

            xlWorkSheet.Shapes.AddPicture("C:\xl_pic.JPG", _
             Microsoft.Office.Core.MsoTriState.msoFalse, _
             Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45)

i got it from

http://vb.net-informations.com/excel-2007/vb.net_excel_2007_insert_picture.htm


thanks


jayeshl replied to subbulakshmi vairavan on 01-Apr-10 12:06 PM
hi all masters,

is given solution works in asp.net web application ?????

give me reply as soon as possible..

thanks
Lalit M replied to song yifeng on 09-Aug-10 07:43 AM
Try this code sample using  Microsoft Excel 12.0 Object Library.

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.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            //add some text 
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
            xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";

            xlWorkSheet.Shapes.AddPicture("C:\\csharp-xl-picture.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45); 

 
            xlWorkBook.SaveAs("csharp.net-informations.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();
            }
        } 

    }
}
John Glenn replied to song yifeng on 01-Dec-11 03:50 AM
Hello,

you can also easily add pictures to your Excel files with this http://www.gemboxsoftware.com/spreadsheet/overview library.

Here is a sample http://www.gemboxsoftware.com/spreadsheet/overview code:

var excelFile = new ExcelFile();

 

var worksheet = excelFile.Worksheets.Add("MyWorksheet");

 

// Add 'Penguins.jpg' picture from cell A1 to cell K11 and let it move and size with cells.

worksheet.Pictures.Add("Penguins.jpg", PositioningMode.MoveAndSize,

    new AnchorCell(worksheet.Columns[0], worksheet.Rows[0], true),

    new AnchorCell(worksheet.Columns[10], worksheet.Rows[10], false));

 

excelFile.SaveXlsx("MyExcelFile.xlsx");