บางครั้งการ export ข้อมูลออกไปเป็นไฟล์ excel ก็ต้องการใส่ภาพลงไปด้วย อาจจะเป็นโลโก้บริษัท หรือว่าเป็นภาพตัวอย่างสินค้า PHPExcel สามารถแทรกภาพได้ทั้งจาก file หรือ url ของภาพที่ออนไลน์อยู่
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <?php include 'vendor/phpoffice/phpexcel/Classes/PHPExcel.php' ; $objPHPExcel = new PHPExcel(); /* Set default style */ $defaultStyle = $objPHPExcel ->getDefaultStyle(); $defaultStyle ->getFont() ->setName( 'Arial' ) ->setSize(11); /* Set document properties */ $title = 'Exports_Datas_' . date ( 'Y-m-d_H:i' ); $objPHPExcel ->getProperties()->setCreator( 'Pitt Phunsanit' ) ->setCategory( 'Exports Datas' ) ->setDescription( $title ) ->setKeywords( 'Exports Datas ' . date ( 'Y-m-d' )) ->setSubject( $title ) ->setTitle( $title ); /* rename sheet */ $objWorkSheet = $objPHPExcel ->getActiveSheet(); $objWorkSheet ->setTitle( 'Exports Datas' ); /* image path */ $images = [ 'vendor/phpoffice/phpexcel/Examples/images/officelogo.jpg' , 'vendor/phpoffice/phpexcel/Examples/images/paid.png' , 'vendor/phpoffice/phpexcel/Examples/images/phpexcel_logo.gif' , 'vendor/phpoffice/phpexcel/Examples/images/termsconditions.jpg' , ]; $rowNo = 0; foreach ( $images as $image ) { $rowNo ++; $coordinate = 'A' . $rowNo ; $objWorkSheet ->getRowDimension( $rowNo )->setRowHeight(90); $objWorkSheet ->setCellValue( $coordinate , $image ); switch ( strtolower ( pathinfo ( $image , PATHINFO_EXTENSION))) { case 'gif' : $gdImage = imagecreatefromgif( $image ); $mimetype = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF; $render = PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF; break ; case 'jpeg' : case 'jpg' : $gdImage = imagecreatefromjpeg( $image ); $mimetype = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG; $render = PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG; break ; case 'png' : $gdImage = imagecreatefrompng( $image ); $mimetype = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG; $render = PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG; break ; } $objDrawing = new PHPExcel_Worksheet_MemoryDrawing(); $objDrawing ->setCoordinates( $coordinate ); $objDrawing ->setImageResource( $gdImage ); $objDrawing ->setWorksheet( $objWorkSheet ); /* optional */ $objDrawing ->setDescription( $image ); $objDrawing ->setMimeType( $mimetype ); $objDrawing ->setName( pathinfo ( $image , PATHINFO_FILENAME)); $objDrawing ->setRenderingFunction( $render ); $objDrawing ->setResizeProportional(true); $objDrawing ->setWidth(100); } $objWorkSheet ->getColumnDimension( 'A' )->setAutoSize(true); /* write */ $objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel , 'Excel2007' ); header( 'Content-Type: application/vnd.ms-excel' ); header( 'Content-Disposition: attachment;filename="' . $title . '.xlsx"' ); header( 'Cache-Control: max-age=0' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Cache-Control: post-check=0, pre-check=0' , false); header( 'Pragma: no-cache' ); $objWriter ->save( 'php://output' ); |
อธิบาย
- setCoordinates
- ตำแหน่งที่ให้ภาพไปลอยอยู่
- setDescription
- รายละเอียด (optional)
- setName
- ชื่อภาพ (optional)
- setResizeProportional
- รักษาสัดส่วนของภาพเอาไว้
- setWorksheet
- อ้างถึง Active Sheet หรือ sheet ที่ใส่ข้อมูล
ถ้าเลือกใช้ PHPExcel_IOFactory::createWriter ผิด version เช่น ‘Excel5’ อาจจะทำให้ภาพมีสัดส่วนเพี้ยนได้