Export two images of PHP Excel file

Source: Internet
Author: User

PHP file export images and text exported simultaneously

In fact, before writing a PHP file export, with this very similar, because the project needs to export the image, query, and wrote a,

This enables the export of images (only local images, cannot use remote image links)


/** * Export the corresponding activity voting record */Public function Exportxls () {$alist = Datafrom db; Obtain the corresponding data//1 from the database.          From the database to get the corresponding two-dimensional array $alist = array (...);          $list = $alist;          $data = Array (); 2.          Set the table header name of xls $HEADARR = Array ("Rank", "name", "Phone", "award", "Participation Time");              if (false = = = Empty ($list)) {$i = 0;                  foreach ($list as $key = + $val) {//assemble the corresponding cell a,b,c,d ...                         $data [$i] = Array (($i + 1),//a $val [' name '],//b                      $val [' Tel '],//c $val [' Award '],//d ...                   );              $i + +; }}else{$data [0] = array (' No related record!          '); } $imgindexs = Array (5);        Put the index of the column that is the picture the first one is 0 this is a one-dimensional array $fileName = "Your name-". Date (' y-m-d ');    $this->output_customer ($HEADARR, $data, $fileName, $imgindexs); } Public FuNction Output_customer ($HEADARR, $alist, $filename, $imgindexs) {set_time_limit (0) ini_set (' Memory_limit ', '-1 '); $ dir = $_server[' Document_root ']; Define the site root directory/** set error level */error_reporting (e_all) ini_set (' display_errors ', TRUE); Ini_set (' Display_startup_errors ', TRUE) if (Php_sapi = = ' cli ') die (' This example should is run from a Web Browser ');/** Include Phpexcel */require_once $dir. '/public/phpexcel/phpexcel.php ';//Create new Phpexcel object$objphpexcel = new Phpexcel ();//Set document Properties$objphpexcel->getproperties ()->setcreator ("Maarten Balliauw")->setlastmodifiedby ("Maarten Balliauw ")->settitle (" Office "xlsx Test Document")->setsubject (Office "xlsx Test Document") SetDescription ("Test document for Office" XLSX, generated using PHP classes. ") ->setkeywords ("Office openxml PHP")->setcategory ("Test result file");/* Instantiate Excel Picture Processing class */$objDrawing = new Phpexcel_worksheet_drawing (); $width =; $colnums = count ($HEADARR); for ($i = 0, $startA = "A"; $i < $colnums; $i + +) {//Set number of columns $temp = Chr (Intval (Ord ($startA)) + $i); $objPHPExcel->getactivesheet ()->getcolumndimension ($t        EMP)->setwidth ($width);} Set the title for ($i = 0, $startA = "A"; $i < $colnums; $i + +) {//Set the number of columns $temp = Chr (Intval (Ord ($startA)) + $i). ' 1 '; $objPHPExcel->setactivesheetindex (0)->setcellvalue ($temp, $HEADARR [$i]);} Miscellaneous glyphs, Utf-8$row=2;foreach ($alist as $val) {//Set line height $objPHPExcel->getactivesheet ()            Getrowdimension ($k)->setrowheight ($span = 0; $startA = ' A ';               Fills the contents of each row foreach ($val as $factval) {$temp = Chr (intval ($startA) + $span). $row; 1. Picture Fill column if (In_array ($span in $imgindexs)) {/* Instantiate Insert Picture class */$objDrawing = new phpexcel_worksheet_drawing ();/* Set Picture path Remember: only local pictures */$objDrawing->setpath ($dir. $factval);/* Set Picture Height */$objDrawing->setheight; $objDrawing SetWidth (50);/* Sets the cell to be inserted in the picture */$objDrawing->setcoordinates ($temp); $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());   }else{//2. Non-picture filled column $objPHPExcel->setactivesheetindex (0)->setcellvalue ($temp, $factval);            } $span + +;}     $row + +; }//Rename worksheet$date = Date (' y-m-d '), $objPHPExcel->getactivesheet ()->settitle ($filename);//Set active sheet        Index to the first sheet, so Excel opens this as the first sheet$objphpexcel->setactivesheetindex (0);        $filename = Iconv ("Utf-8", "gb2312", $filename. Date (' y-m-d '));        Header (' content-type:application/vnd.ms-excel; '); Header (' Content-disposition:attachment;filename= '. $filename. XLS "'); header (' cache-control:max-age=0 ');//If you ' re serving to IE 9 and then the following is Neededheader (' Cache-cont Rol:max-age=1 ')//If you ' re serving to IE over SSL and then the following is Neededheader (' Expires:mon, Jul 1997 0 5:00:00 GMT '); Date in the Pastheader (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); Always Modifiedheader (' Cache-control:cache, Must-revalidate '); //Http/1.1header (' pragma:public '); http/1.0//Note Here The second argument written ' Excel2007 ' will avoid special characters or chinese garbled $objwriter = Phpexcel_iofactory::createwriter ($objPHPExce L, ' Excel2007 '); $objWriter->save (' Php://output '); exit;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Export two images of PHP Excel file

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.