Solved

Add image to Excel sheet using .Net

Posted on 2009-04-09
4
315 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
ID: 24107210
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
ID: 24113599
Any other solution without paste.
0
 
LVL 6

Accepted Solution

by:
HarryNS earned 250 total points
ID: 24128592
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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