Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
var sd = new ExcelLibrary.SpreadSheet.Picture();

sd.Image = ExcelLibrary.SpreadSheet.Image.FromFile("c:\\images\\a.jpg");
ws.AddPicture(sd);


is not working, it gives unsupported format error.

how do i do it from file and byte[]??
Posted
Comments
What is ws?
scorpzonex 4-Jul-14 15:05pm    
worksheet
ExcelLibrary.SpreadSheet.Worksheet
ridoy 4-Jul-14 15:50pm    
Do you check it? http://www.codeproject.com/Questions/84487/How-to-insert-a-picture-image-in-XML-spreadsheet
scorpzonex 4-Jul-14 18:06pm    
for.png works but .jpg fails and how to it from a memorystream (variable)?

1 solution

Do code
Code to Export to Excel using ExcelLite.
WriteableBitmap bitmap = new WriteableBitmap(640,300);
 
//pass the chart control to convert it into Bitmap
 bitmap.Render(chart1, null);
 bitmap.Invalidate();
 
// open file dialog for selecting export file
 SaveFileDialog sDialog = new SaveFileDialog();
 sDialog.Filter = "Excel Files(*.xls)|*.xls";
 
if (sDialog.ShowDialog() == true)
 {
 // create a workbook object
 Workbook workbook = new Workbook();
 //Create a worksheet object
 Worksheet worksheet1 = new Worksheet("SheetWithImage");
 
// creat a spreadsheet picture object
 Lite.ExcelLibrary.SpreadSheet.Picture pic = new Lite.ExcelLibrary.SpreadSheet.Picture();
 //set its image property from silverlight image control
 // image formates 0xF01E=png,0xF01D=jpeg
 //ImageTranslator.TranslateImageToBytes translate an image control to byte array
 // that will be used by excel picture object to plot picture in excel file.
 System.Windows.Controls.Image image = new System.Windows.Controls.Image();
 image.Source = bitmap;
 image.Name = "imgExport";
 image.Width = 640;
 image.Height = 300;
 image.Stretch = Stretch.Fill;
 pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(ImageTranslator.TranslateImageToBytes(image), 0xF01E);
 
 //set picture size
 pic.TopLeftCorner = new CellAnchor(1, 1, 10, 10);
 pic.BottomRightCorner = new CellAnchor(8, 5, 10, 10);
 // add picture to spreadsheet
 worksheet1.AddPicture(pic);
 /// add worksheet to workbook
 workbook.Worksheets.Add(worksheet1);
 // get the stream of selected file
 Stream sFile = sDialog.OpenFile();
 // save excel file
 workbook.Save(sFile);
 }
 
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