Insert image in excel file and file size growing..

Nov 8, 2012 at 12:13 PM

Hi all,

first of all, the script is working and it is doing what i want. But i have a "problem" with file size.

In details:

i'm inserting some images (logos..) in excel files with this code

          $objDrawing = new PHPExcel_Worksheet_Drawing();

As i said before, the file is correct and images are in the right place.

The problem is that the file size is strange.

I mean:

the file with no logo is 54k

The file with logos as created by phpexcel is 1376k (lol)

If i open this file with excel and simply SAVE it, it becomes 338k (5 sheets and 4 images in every sheet, it can be correct..)

So, i'm wondering if i'm missing something in my code, because it's obvious that PHPexcel is doing something strange..

Thanks a lot


Nov 8, 2012 at 2:12 PM
MarkBaker wrote:

If the worksheet contains the data that you expect, then you really shouldn't worry about this. 4MB isn't particularly large for a file.

PHPExcel is not a file editor: it doesn't simply add a few extra cells into the original file that might simply add a few bytes to the resultant size. It loads the file, converting its content to a PHPExcel object; you modify that PHPExcel object; and when you save, it writes a new file from the content of that PHPExcel object. Excel itself has a lot of different options for the way it stores the spreadsheet data, and the resources to optimise to store them extremely efficiently when it saves. PHPExcel stores in a generic manner, without the overhead of determining how best to optimise the resultant file to a minimal size. PHPExcel may, for example, save additional default styles that aren't used in the workbook, whereas Excel itself may discard these. You don't mention which format you're using: .xlsx or .xls... the former is zip compressed; but the zip compression method used by ZipArchive may not match the compression method used by Excel.

So there's a whole host of factors that come into play that affect the size of a saved file: what's generally more important is that the content is correct.

This is the answer that Mark gave me to my question because I was having a similar problem when opening a .xlt template (Excel5 format) and saving as a .xls (Excel5).  If you are using the newer format, Excel2007, then hopefully he will have a different answer that could help you out.


~ Mike

Nov 8, 2012 at 9:35 PM

The same answer applies whether saving using the Excel5 Writer or the Excel2007 Writer. The primary concern of PHPExcel is to provide functionality, to ensure that if you add an image in PHPExcel you get that image when you open the file in MS Excel.

If you open and save using Open/LibreOffice or even a different version of MS Excel, you'll probably find differences in size, though perhaps less drastic.