Phpexcel file export II Image export-php Tutorial

Source: Internet
Author: User
Phpexcel file export 2 image export php file export the image and text export at the same time

In fact, I wrote a php file for export, which is very similar to this, because the project needs to export, query, and write another,

This can export images (only local images are supported, and remote image links are not supported)


/*** Export the corresponding activity voting record */public function exportxls () {$ alist = datafrom db; // obtain the corresponding data from the database // 1. obtain the corresponding two-dimensional array from the database $ alist = array (...); $ list = $ alist; $ data = array (); // 2. set the xls header name $ headArr = array ("ranking", "name", "mobile phone", "award", "participation time "); if (false = empty ($ list) {$ I = 0; foreach ($ list as $ key => $ val) {// assemble the corresponding cell, b, C, D... $ Data [$ I] = array ($ I + 1), // A $ val ['name'], // B $ val ['tel'], // C $ val ['award '], // D ...); $ I ++ ;}} else {$ data [0] = array ('no relevant records! ') ;}$ Imgindexs = array (5); // The first index in the image column 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']; // defines the website ROOT directory/** sets the error level */error_reporting (E_ALL ); ini_set ('display _ errors ', TRUE); ini_set ('display _ startup_errors', TRUE); if (PHP_SAPI = 'cl ') die ('This example shocould only be 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 2007 XLSX Test Document")-> setSubject ("Office 2007 XLSX Test Document ") -> setDescription ("Test document for Office 2007 XLSX, generated using PHP classes. ")-> setKeywords (" office 2007 openxml php ")-> setCategory (" Test result file ");/* instantiate an excel image processing class */$ objDrawing = new PHPExcel_Worksheet_Drawing (); $ width = 25; $ colnums = count ($ headArr); for ($ I = 0, $ startA = "A"; $ I <$ colnums; $ I ++) {// set the number of columns $ temp = chr (intval (ord ($ startA) + $ I); $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ($ temp) -> 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 the row height $ objPHPExcel-> getActiveSheet ()-> getRowDimension ($ k)-> setRowHeight (50); $ span = 0; $ startA = 'a'; // fill in the content of each row foreach ($ val as $ factval) {$ temp = chr (intval (ord ($ startA )) + $ span ). $ row; // 1. if (in_array ($ span in $ imgindexs) {/* instantiate and insert an image class */$ objDrawing = new PHPExcel_Worksheet_Drawing ();/* Set the image path to remember: only local images */$ objDrawing-> setPath ($ dir. $ factval);/* Set the image height */$ objDrawing-> setHeight (50); $ objDrawing-> setWidth (50 ); /* Set the cell to insert an image */$ objDrawing-> setCoordinates ($ temp); $ objDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());} else {// 2. non-image filling 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='.w.filename.'.xls "'); header ('cache-Control: max-age = 0 '); // If you're serving to IE 9, then the following may be neededheader ('cache-Control: max-age = 1 '); // If you're serving to IE over SSL, then the following may be neededheader ('expires: Mon, 26 Jul 1997 05: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.1 header ('pragma: public '); // HTTP/1.0 // note that writing the second parameter 'excel2007 'will avoid special characters or Chinese garbled characters $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); $ objWriter-> save ('php: // output'); exit ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.