user1040975
user1040975

Reputation: 440

I'm trying to insert an image into an excel document using ExcelLibrary with Picture and CellAnchor and the image doesn't display

I trimmed out quite a bit so if there is context missing sorry, I'll edit.

I'm not getting any errors and the Picture does seems to get added to the workbook object...

I've also tried just inserting into a new cell(), but when I get to saving to the stream it get an Invalid Cell value.

I'm using this link as a reference: https://excellibrary.googlecode.com/svn-history/r51/trunk/src/ExcelLibrary.WinForm/Form1.cs

Workbook wBook = new Workbook();
Worksheet wSheet = new Worksheet();
//getting my file
var path = context.Server.MapPath("~/data/default/content/logo.bmp");
pic.Image = ExcelLibrary.SpreadSheet.Image.FromFile(path);
pic.TopLeftCorner = new CellAnchor(5, 1, 0, 0);
pic.BottomRightCorner = new CellAnchor(7, 5, 592, 243);

wSheet.AddPicture(pic);
wSheet.Cells[rows, 0] = new Cell("some text");
wBook.Worksheets.Add(wSheet);

context.Response.AddHeader("Content-Disposition","Attachment;Filename=PlattShoppingCart.xls");
context.Response.ContentType = "application/vnd.ms-excel";
MemoryStream stream = new MemoryStream();
wBook.SaveToStream(stream);
context.Response.BinaryWrite(stream.ToArray());

Upvotes: 1

Views: 1421

Answers (2)

user1040975
user1040975

Reputation: 440

I don't know if it was just me or it's actually not anywhere in the documentation, but *.bmp, *.jpeg, *.gif', are not supported and it worked by simply turning it into a .png....

Thanks for the link though as it was an experience to compare it to other methods. Here are some other links if you find yourself getting this far and you still have issues:

http://www.codeproject.com/Questions/793324/How-Do-I-Insert-Jpg-Image-From-File-Stream

http://www.codeproject.com/Questions/84487/How-to-insert-a-picture-image-in-XML-spreadsheet

How Do I Create a System.Windows.Media.ImageSource From a Byte Array?

Upvotes: 0

Owen Ivory
Owen Ivory

Reputation: 244

This question and answer uses Microsoft.Interop Excel, but it accomplishes the same task. Probably not the answer you need, but I got it to work just fine. Of course, I was already using the interop stuff, just needed to modify the code a little to get it work.

I wonder if you forgot to add the ExcelLibrary equivalent of

pic.Placement = // Can be any of Excel.XlPlacement.XYZ value

Upvotes: 1

Related Questions