PHPExcel无法加入图片
时间:2021-07-01 10:21:17
帮助过:64人阅读
求助:PHPExcel无法加入图片
$objPHPExcel = new PHPExcel();
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objPHPExcel->setActiveSheetIndex(0);
$objDrawing->setPath('D:\wwwroot\user\upimg\201460dZeETjg_1.jpg');
$objDrawing->setCoordinates("A1");
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
返回错误:Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.' in D:\wwwroot\user\upimg\Classes\PHPExcel\Worksheet\BaseDrawing.php:240 Stack trace: #0 D:\wwwroot\user\upimg\xls.php(71): PHPExcel_Worksheet_BaseDrawing->setWorksheet(Object(PHPExcel_Worksheet)) #1 {main} thrown in D:\wwwroot\user\upimg\Classes\PHPExcel\Worksheet\BaseDrawing.php on line 240
------解决方案--------------------
对呀!
你的 $objDrawing = new PHPExcel_Worksheet_Drawing(); 在循环外面
而 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 在循环里面
你如何能保证 $objDrawing = new PHPExcel_Worksheet_Drawing(); 只被这行一次呢?
虽然是套在条件分支中的,你有如何能保证条件只成立一次呢?
你只需把 $objDrawing = new PHPExcel_Worksheet_Drawing(); 放到循环里面,一切都解决了