Solved

How to insert a picture into Excel file and save the file with the picture in C#?

Posted on 2006-06-09
6
8,518 Views
Last Modified: 2008-01-09
I created an Excel file using C# and I need to insert a picture in it and save the file with the picture.

I used the following code to insert a picture into a cell, and it worked - the picture was inserted into the opened file
                        oXL = new Excel.Application();
                        //Get a new workbook.
                        oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
                        oSheetP = (Excel._Worksheet)oWB.Worksheets[1];
                        oXL.Visible = true;

                                                                //populate the file below
                                                                // ....................some code
                                                               
                                                                // insert a picture
                        string thelogo = currentDir + "\\images\\logo.gif";
                        System.Drawing.Image oImage = System.Drawing.Image.FromFile(thelogo);
                        Excel.Range imgRange;

                        imgRange = (Excel.Range)oSheetP.Cells[1, 12];
                        System.Windows.Forms.Clipboard.SetDataObject(oImage, true);
                        oSheetP.Paste(imgRange, thelogo);

But then I used the following code to save the file, and the picture was not saved.  (In the process of file saving, I was notified by the prompt box that the picture will be lost, and it was lost.)
                  oWB.SaveAs(MyFileName, Excel.XlFileFormat.xlXMLSpreadsheet,
                                    System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                    System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                    Excel.XlSaveAsAccessMode.xlNoChange,
                                    System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                    System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                    System.Reflection.Missing.Value);

How can I save the file to preserve a picture in it?
Do I need to use another code to insert a picture or another code to save the file?

Please not that when I just tried to save the opened file by clicking on File/Save As on the Excel menu, the file was saved, and the picture was saved as well.  So the picture can be saved.  But I need to save the file using C# code, and it is just that this Workbook.SaveAs( ) method does not do the work.

Please suggest a solution
0
Comment
Question by:pavelmed
  • 4
6 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16871842
Hi pavelmed,
----------

you are using excel xml format in the save method is that what you want? if you use the normal excel save from the file menu did you use xml as the file format too?

----------
bruintje
share what you know, learn what you don't
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 250 total points
ID: 16872092
When I save an Excel file as an XML spreadsheet, I get the following warning:

The following features from your workbook will not be saved in the XML Spreadsheet:

* AutoShapes, other objects, or charts

Do you want to continue saving as an XML Spreadsheet?


So it appears that XML spreadsheets do not support embedded objects (including images).
0
 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 250 total points
ID: 16872155
no indeed, normal xml does not store images either if it at all they will be stored as link, xml is purely a text format and images are not embedded
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:bruintje
ID: 16872165
they are probably stored in a seperate folder with the file as the html format did when you save a excel sheet as webpage
0
 

Author Comment

by:pavelmed
ID: 16872195
Thanks to both of you!
I did not want to save the file as XML format, I just did not notice that it was saved this way.
I changed the format to xlExcel9795, and it worked!

Thank you for answering so quickly!

I'll split points since you both helped me.  Have a good day!
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16872210
great it worked for you, thanks for the grade :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Anyway to make "All" the default in the dropdown? 6 34
SQL Login 17 38
C# Single Form 8 27
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

18 Experts available now in Live!

Get 1:1 Help Now