pavelmed
asked on
How to insert a picture into Excel file and save the file with the picture in C#?
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.Work books.Add( Missing.Value ));
oSheetP = (Excel._Worksheet)oWB.Work sheets[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.FromF ile(thelog o);
Excel.Range imgRange;
imgRange = (Excel.Range)oSheetP.Cells [1, 12];
System.Windows.Forms.Clipb oard.SetDa taObject(o Image, 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.xlXMLSp readsheet,
System.Reflection.Missing. Value, System.Reflection.Missing. Value,
System.Reflection.Missing. Value, System.Reflection.Missing. Value,
Excel.XlSaveAsAccessMode.x lNoChange,
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
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.Work
oSheetP = (Excel._Worksheet)oWB.Work
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.FromF
Excel.Range imgRange;
imgRange = (Excel.Range)oSheetP.Cells
System.Windows.Forms.Clipb
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.xlXMLSp
System.Reflection.Missing.
System.Reflection.Missing.
Excel.XlSaveAsAccessMode.x
System.Reflection.Missing.
System.Reflection.Missing.
System.Reflection.Missing.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
they are probably stored in a seperate folder with the file as the html format did when you save a excel sheet as webpage
ASKER
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!
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!
great it worked for you, thanks for the grade :)
----------
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