: PHPExcel cannot add images

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


Error returned: 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): Response-> 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 are only a part. you can't see the problem from this point!
This code snippet is correct!

Uncaught exception 'exception' with message 'A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet .'
Yes: a PHPExcel_Worksheet_Drawing instance can only exist in one cell.

Apparently, you put $ objDrawing in A1 and put it elsewhere.

/** 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 shocould only be 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 2007 XLSX Document ")
-> SetSubject ("Office 2007 XLSX Document ")
-> SetDescription ("document for Office 2007 XLSX, generated using PHP classes .")
-> SetKeywords ("office 2007 openxml php ")
-> SetCategory ("result file ");


// Add some data
$ ObjPHPExcel-> setActiveSheetIndex (0)
-> SetCellValue ('A1 ')
-> SetCellValue ('b1 ', 'name ')
-> SetCellValue ('C1', 'height ')
-> SetCellValue ('d1 ', 'sanwei ')
-> SetCellValue ('e1 ', 'telephony ')
-> SetCellValue ('F1', 'Introduction ')
-> SetCellValue ('G1', 'datamount ')
-> SetCellValue ('h1 ', 'body ')
-> SetCellValue ('i1', 'body ')
-> SetCellValue ('j1', 'recommender ')
-> SetCellValue ('k1 ', 'status ');
// 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 ['tids'])
//-> SetCellValue ("G". $ k ,)
//-> SetCellValue ("H". $ k, '../upimg/'.w.row='nameid'}.'_2.jpg ')
//-> SetCellValue ("I". $ k, '../upimg/'.w.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, using 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, using 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 table ');


// 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 = ".xls "');
Header ('cache-Control: max-age = 0 ');
// If you're serving to IE 9, then the following may be needed
Header ('cache-Control: max-age = 1 ');

// If you're serving to IE over SSL, then the following may be needed
Header ('expires: Mon, 26 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 the loop
$ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet (); in the loop
How can you ensure that $ objDrawing = new PHPExcel_Worksheet_Drawing (); is this row only used once?
Although it is set in the condition branch, how do you ensure that the condition is valid only once?

You just need to put $ objDrawing = new PHPExcel_Worksheet_Drawing (); in the loop, everything is done.

Thanks for finding the problem, but the xls document cannot be opened.
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.