: Phpexcel Cannot add image

Source: Internet
Author: User
$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 ());


Return Error: Fatal error:uncaught exception ' phpexcel_exception ' with message ' A Phpexcel_worksheet have 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: phpexcel_worksheet_basedrawing-> Setworksheet (Object (phpexcel_worksheet)) #1 {main} thrown in D:\wwwroot\user\upimg\Classes\PHPExcel\Worksheet\ basedrawing.php on line 240


Reply to discussion (solution)

You're just a fragment, you can't see the problem from this point!
Because this code fragment is not wrong!

Uncaught exception ' exception ' with message ' A Phpexcel_worksheet have already been assigned. Drawings can only exist on one phpexcel_worksheet. '
Is that a phpexcel_worksheet_drawing instance can only exist in one cell

Apparently, you put $objDrawing behind the A1, and you put it somewhere else.

/** Error Reporting */
Error_reporting (E_all);
Ini_set (' display_errors ', TRUE);
Ini_set (' display_startup_errors ', TRUE);
Date_default_timezone_set (' Europe/london ');

if (Php_sapi = = ' CLI ')
Die (' This example should only is run from a Web Browser ');

/** Include Phpexcel */
Require_once dirname (__file__). '/classes/phpexcel.php ';

$db = new mysqli (' localhost ', ' user.com ', ' sumtohqhpw ', ' users ');
Mysqli_query ($db, "set character set ' UTF8 '");


Create New Phpexcel Object
$objPHPExcel = new Phpexcel ();

Set Document Properties
$objPHPExcel->getproperties ()->setcreator ("FNSR")
->setlastmodifiedby ("FNSR")
->settitle (Office "XLSX Document")
->setsubject (Office "XLSX Document")
->setdescription ("Document for Office" XLSX, generated using PHP classes. ")
->setkeywords ("Office openxml PHP")
->setcategory ("result file");


ADD some data
$objPHPExcel->setactivesheetindex (0)
->setcellvalue (' A1 ', ' No. ')
->setcellvalue (' B1 ', ' name ')
->setcellvalue (' C1 ', ' height ')
->setcellvalue (' D1 ', ' measurements ')
->setcellvalue (' E1 ', ' phone ')
->setcellvalue (' F1 ', ' Introduction ')
->setcellvalue (' G1 ', ' Big Head ')
->setcellvalue (' H1 ', ' bust ')
->setcellvalue (' I1 ', ' Whole body ')
->setcellvalue (' J1 ', ' referrer ')
->setcellvalue (' K1 ', ' state ');
Miscellaneous Glyphs, UTF-8
$query = "SELECT * from Enroll";
$result = $db->query ($query);
$num _results = $result->num_rows;
$k = 2;
$objDrawing = new phpexcel_worksheet_drawing ();
for ($i =0; $i < $num _results; $i + +) {
$row = $result->fetch_assoc ();
$objPHPExcel->setactivesheetindex (0)
->setcellvalue ("A". $k, $row [' NameID '])
->setcellvalue ("B". $k, $row [' pname '])
->setcellvalue ("C". $k, $row [' stature '])
->setcellvalue ("D". $k, $row [' bust '])
->setcellvalue ("E". $k, $row [' Stel '])
->setcellvalue ("F". $k, $row [' tips '])
->setcellvalue ("G". $k,)
->setcellvalue ("H". $k, '.. /upimg/'. $row [' NameID ']. ' _2.jpg ')
->setcellvalue ("I". $k, '.. /upimg/'. $row [' NameID ']. ' _3.jpg ')
->setcellvalue ("J". $k, $row [' refs '])
->setcellvalue ("K". $k, $row [' State ']);

$pa =$_server[' document_root ']. ' \\mweb\\tjnsr\\upimg\\ ';
$pa = '. /upimg/';
if (file_exists ($row [' NameID ']. ' _1.jpg ')) {
$objPHPExcel->setactivesheetindex (0);
$objDrawing->setpath (' D:\wwwroot\user\upimg\201460dZeETjg_1.jpg ');
$objDrawing->setcoordinates ("G1");
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
}
/*if (file_exists ($row [' NameID ']. ' _2.jpg ')) {
$objDrawing->setpath ($row [' NameID ']. ' _2.jpg ');
$objDrawing->setcoordinates ("H". $k);
$objPHPExcel->setactivesheetindex (0)
->setcellvalue ("H". $k, $row [' NameID ']. ' _2.jpg ');
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
}
if (file_exists ($row [' NameID ']. ' _3.jpg ')) {
$objDrawing->setpath ($row [' NameID ']. ' _3.jpg ');
$objDrawing->setcoordinates ("I". $k);
$objPHPExcel->setactivesheetindex (0)
->setcellvalue ("I". $k, $row [' NameID ']. ' _3.jpg ');
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
}*/
$k + +;
}
$result->free ();
$db->close ();

$objPHPExcel->setactivesheetindex (0)
->setcellvalue (' A5 ', $PA);
Rename Worksheet
$objPHPExcel->getactivesheet ()->settitle (' contact form ');


Set Active sheet Index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setactivesheetindex (0);


Redirect output to a client ' s Web browser (EXCEL5)
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= ' registration form. xls "');
Header (' cache-control:max-age=0 ');
If you ' re serving to IE 9 and then the following is needed
Header (' cache-control:max-age=1 ');

If you ' re serving to IE over SSL and then the following is needed
Header (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the past
Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); Always modified
Header (' Cache-control:cache, Must-revalidate '); http/1.1
Header (' Pragma:public '); http/1.0

$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
Exit

Yes!
Your $objDrawing = new phpexcel_worksheet_drawing (); Outside of the loop
and $objDrawing->setworksheet ($objPHPExcel->getactivesheet ()); Inside the loop.
How can you guarantee $objDrawing = new phpexcel_worksheet_drawing (); Only this line once?
Although it is set in the conditional branch, how can you guarantee that the condition is only set once?

All you need to do is $objDrawing = new phpexcel_worksheet_drawing (); Put it in the loop, everything's settled.

Thank you, the problem is found, but the XLS document is not open
Code modification: if (file_exists ($row [' NameID ']. ' _1.jpg ')) {
$objDrawing [$k] = new phpexcel_worksheet_drawing ();
$objDrawing [$k]->setpath ($row [' NameID ']. ' _1.jpg ');
$objDrawing [$k]->setcoordinates ("G". $k);
$objDrawing [$k]->setworksheet ($objPHPExcel->getactivesheet ());
}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.