使用PHPExcel在循环中将图像存储在Excel工作表中

时间:2015-10-14 16:32:03

标签: phpexcel

我正在尝试使用PHPExcel将图像保存在Excel文件中。这是代码的一部分 -

for($i=0; $i< $count; $i++)
{   
        $objPHPExcel = PHPExcel_IOFactory::load("file.xlsx");        
        // Code generates url (of the image) corresponding to this iteration of the loop and saves it in the variable $url. $string is the name of the image to be saved.
        file_put_contents('photos/'.$string.'.jpg', file_get_contents($url));
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(22);
        $gdImage = imagecreatefromjpeg($url);
        $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
        $objDrawing->setImageResource($gdImage);
        $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
        $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
        $objDrawing->setHeight(150);
        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
        $objDrawing->setCoordinates('B'.$i);
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter->save('file.xlsx');
    }

循环执行一次,但在第二次迭代中,它会生成以下错误 -

致命错误:/ Applications / XAMPP中未显示异常'PHPExcel_Writer_Exception',消息'文件zip:///Applications/XAMPP/xamppfiles/htdocs/webscraping/file.xlsx#xl/media/ca8687837091b28ca7245ec6b0b5be751.png'不存在/xamppfiles/htdocs/webscraping/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php:195堆栈跟踪:#0

如何删除此错误?

0 个答案:

没有答案