[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Add image to Excel sheet using .Net

Posted on 2009-04-09
4
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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