简体   繁体   中英

PHPExcel generating large files from small amounts of data

I open a 101k Excel 5 spreadsheet using PHPExcel version 1.8. I update certain cells, adding a tiny amount of data. When I write the spreadsheet back out to a new file, the content looks correct, but it is 3,592k in size.

I tried opening up the original spreadsheet, copying the cells from the updated spreadsheet to the original and saving as a new name. The resulting size is actually smaller, 96k.

Why does PHPExcel make the file so much larger and how can I fix this? Below is my code.

$objReader = PHPExcel_IOFactory::createReader( 'Excel5' );
$objPHPExcel = $objReader->load( $template_location . $template_file_name );

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue( "B17", $last_name )
            ->setCellValue( "C17", $first_name )
            ->setCellValue( "D17", $phone );

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save( $eapis_location . $output_file_name );

The answer is because PHPExcel doesn't apply any of the optimisations that MS Excel itself applies to keep the file size small, mainly to avoid the performance overheads. As a couple of examples, MS Excel doesn't store all its default styles, PHPExcel stores the complete set of default styles. MS Excel uses UTF-8 for all string data, MS Excel uses whatever charset it is configured to use.

The only way you can "fix" this is by writing your own Excel reader/writer; or using something like COM that relies on MS Excel itself.

If PHPExcel creates a file in a format that complies with the BIFF file format, and MS Excel can read it correctly, then is the difference in filesize really a critical problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM