我的VPS具有8GB内存
当我尝试编写4000行和62列时。 1列包含图片(PHPExcel_Worksheet_MemoryDrawing)
写了一些数据后,操作系统已经杀死了后台脚本,因为VPS没有更多的内存。
我正在使用PHPExcel 1.7.8版本
我在每个循环中都写了excel文件。
图片编写示例代码
$gdImage = imagecreatefromstring(file_get_contents($proof_photo_1));
if ($gdImage) {
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setImageResource($gdImage);
$objDrawing->setCoordinates("BA" . $i);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing->setHeight(150);
$width = $objDrawing->getWidth() * 0.143;
$height = $objDrawing->getHeight() * 0.75;
if ($rowDim->getRowHeight() < $height) {
$rowDim->setRowHeight($height);
}
$colDimSh = $objPHPExcel->getActiveSheet()->getColumnDimension("BA");
$colDimSh->setAutoSize(false);
$colDim = $objPHPExcel->getActiveSheet()->getColumnDimension("BA");
if ($colDim->getWidth() < $width) {
$colDim->setWidth($width);
}
unset($objDrawing);
} else {
$objPHPExcel->getActiveSheet()->SetCellValue("BA" . $i, "-");
}
我已经尝试过很多方法来节省内存
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize' => '100MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);
在每个循环中
if($i % 100 == 0){
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($csvPath);
$objPHPExcel->disconnectWorksheets();
unset($objWriter);
unset($objPHPExcel);
chmod($csvPath, 0777);
$objPHPExcel = PHPExcel_IOFactory::load($csvPath);
$objPHPExcel->setActiveSheetIndex(0);
}
上面花费很多时间来写入excel文件,有时甚至会因为文件重置而重置文件。