Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to add an image to Excel/CSV

by johnnywang (Priest)
on Oct 27, 2005 at 23:45 UTC ( [id://503508]=perlquestion: print w/replies, xml ) Need Help??

johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've been able to create Excel spreed sheets using Win32::OLE, is there a way to insert an image (say .png) in the spreedsheet? how to specify its location? Since CSV files are usually openned up by Excel also, is there anyway to include image in them too? Thanks.

Replies are listed 'Best First'.
Re: How to add an image to Excel/CSV
by bmann (Priest) on Oct 28, 2005 at 00:41 UTC
    You can insert an image into an Excel file, but not a csv. Excel will strip the image out and only save the data when it is saved to csv.

    To see how it's done in an xls, fire up the macro recorder in Excel then insert your image. Now move the image. Stop the macro recorder and open up Excel's VBA IDE. Take the generated code and convert it to perl.

    use strict; use warnings; use Win32::OLE; my $excel = Win32::OLE->CreateObject('Excel.Application'); $excel->{Visible} = 1; my $wb = $excel->Workbooks->Add; $wb->ActiveSheet->Pictures->Insert( "C:\\s\\x.png" )->Select; $excel->Selection->ShapeRange->IncrementLeft( 40 ); $excel->Selection->ShapeRange->IncrementTop( 40 ); __END__ Sub Macro1() ' Macro recorded with Macro Recorder ' Tools -> Macro -> Record new macro ActiveSheet.Pictures.Insert( _ "C:\s\x.png").Select Selection.ShapeRange.IncrementLeft 40 Selection.ShapeRange.IncrementTop 40 End Sub
    Voila, done! BTW, you probably want to read the definitive thread on Excel and Win32::OLE, Using Win32::OLE and Excel - Tips and Tricks.
Re: How to add an image to Excel/CSV
by EvanCarroll (Chaplain) on Oct 27, 2005 at 23:50 UTC
    No, It is almost surely outside of the range of Excel's capabilities to be able to put a picture in a CSV. Excel's CSVs IIRC do not escape commas at all, never mind perform the voodoo required to be to hold binary data. The problem is anytime a binary 44 is viewed as txt and in ASCII/Latin1/UTF8 encoding it is read as a comma. and will therefore bork Excel. Any sufficiently large image, will satisify this condition.


    Evan Carroll
    www.EvanCarroll.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://503508]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found