Solved

Add image to Excel sheet using .Net

Posted on 2009-04-09
4
303 Views
Last Modified: 2012-06-22
I am developing Web application in which file download facility. Before file download image added to specified cell. But in my case it add to A1 cell.
0
Comment
Question by:nahanfee
  • 2
4 Comments
 
LVL 6

Expert Comment

by:HarryNS
Comment Utility
Add Microsoft Excel Object as reference to your application and use the following code. This will work.
using A = Microsoft.Office.Interop.Excel;

        

        private void insertImage()

        {

            A.Application ThisApplication = new A.ApplicationClass();

            A.Workbook ThisWorkBook;
 
 

            string _stFileName = "C:\\Hari\\Test.xls";

            string _stPicture = @"C:\Documents and Settings\149525\My Documents\My Pictures\check.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);

                A.Worksheet ThisSheet = (A.Worksheet)ThisWorkBook.Sheets[2];
 

                ThisSheet.Cells[1, 1] = "test";
 

                A.Range oRange = (A.Range)ThisSheet.Cells[1, 2];
 

                Image oImage = Image.FromFile(_stPicture);

                System.Windows.Forms.Clipboard.SetDataObject(oImage, true);

                ThisSheet.Paste(oRange, _stPicture);
 

                ThisWorkBook.Save();

            }

            catch (Exception oEx)

            {

            }

            finally

            {

                ThisApplication.Quit();

                ThisWorkBook = null;

                ThisApplication = null;

            }
 

        }

Open in new window

0
 

Author Comment

by:nahanfee
Comment Utility
Any other solution without paste.
0
 
LVL 6

Accepted Solution

by:
HarryNS earned 250 total points
Comment Utility
Then you can use this code,
private void insertImage()

        {

            A.Application ThisApplication = new A.ApplicationClass();

            A.Workbook ThisWorkBook;
 
 

            string _stFileName = "C:\\Hari\\Test.xls";

            string _stPicture = @"C:\Documents and Settings\149525\My Documents\My Pictures\check.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);

                A.Worksheet ThisSheet = (A.Worksheet)ThisWorkBook.Sheets[2];
 

                ThisSheet.Cells[1, 1] = "test";

                ThisSheet.Shapes.AddPicture(_stPicture, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 10, 10, 70, 70);
 

                //A.Range oRange = (A.Range)ThisSheet.Cells[1, 2];
 

                //Image oImage = Image.FromFile(_stPicture);

                //System.Windows.Forms.Clipboard.SetDataObject(oImage, true);

                //ThisSheet.Paste(oRange, _stPicture);
 

                ThisWorkBook.Save();

            }

            catch (Exception oEx)

            {

            }

            finally

            {

                ThisApplication.Quit();

                ThisWorkBook = null;

                ThisApplication = null;

            }
 

        }

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now