Click here to Skip to main content
15,886,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write this code for insert an image in excel's file:

C#
// ok lines
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
 object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

// line to modify
xlWorkSheet.Shapes.AddPicture(@"C:\Users\fedand\Pictures\TElogoSquare.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 450, 0, 45, 45); 


I'd like to use an image in resource.
I have to change the string "C:\Users\fedand\Pictures\TElogoSquare.jpg", but how?

thanks
Posted

In addition to Solution 1:

Yes, both AddPicture* methods required file name:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture.ASPX[^],
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture2.ASPX[^].

So, probably the only thing you can do is to save your resource in a temporary file, use this file and remove it later. First, to get a resource, look at the auto-generated resource file (I assume you use .resx resource). You should better create a resource from an image file created in your project, using "Add Existing File". In the auto-generated C# file, you will find a static property of the type Image with the name similar to the name of the original file. Just use it. This is how you save it in a file:
http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx[^].

This is how you can get a file name for a temporary file: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^].

And this is how you delete it: http://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.110%29.aspx[^].

[EDIT]

Alternatively, consider using Open XML SDK instead of Office Interop. Here are the warning from Microsoft against using Interop:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

Please see this answer by Mika Wendelius: Creating basic Excel workbook with Open XML[^].

See also my past answer: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 30-Jul-14 14:26pm    
+5!
Sergey Alexandrovich Kryukov 30-Jul-14 16:12pm    
Thank you, Maciej.
—SA
Since the AddPicture method requires a filename as the first parameter you cannot do what you are asking.
 
Share this answer
 
Comments
Maciej Los 30-Jul-14 14:27pm    
Short and to the point, +5!
Richard MacCutchan 31-Jul-14 2:27am    
Thanks.
Sergey Alexandrovich Kryukov 30-Jul-14 16:13pm    
Agree (voted 4), but, in principle, it could be done by creation of a temporary file. Also, maybe the use of Open XML SDK would offer better options, not sure.
—SA
Richard MacCutchan 31-Jul-14 2:28am    
Of course that is correct.
It's not possible to use an internal resourse?
I thought that my was a very easy question!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900