Home  >  Article  >  php教程  >  Thinkphp3.2 and phpexcel export with generated pictures perfect case

Thinkphp3.2 and phpexcel export with generated pictures perfect case

WBOY
WBOYOriginal
2016-07-06 13:29:211236browse

Thinkphp3.2 and phpexcel export with generated pictures
I am a newbie and have been using tp for a month. Recently, the company required that the report export should have pictures, so I took the time to study it and look at the example code // Export exl <br> Public function look_down(){<br>          $id = I('get.id');<br>          $m = M ('offer_goods');<br>          $where['offer_id'] = $id;<br>          $data = $m->field('goods_id,goods_sn,goods_name,barcode,goods_type,price')->select();<br>                                                                  // Export Exl<br>           import("Org.Util.PHPExcel");<br>           import("Org.Util.PHPExcel.Worksheet.Drawing");<br> Import("Org.Util.PHPExcel.Writer.Excel2007");<br> $objPHPExcel = new PHPExcel();<br>                                                           $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);<br>  <br> $objActSheet = $objPHPExcel->getActiveSheet();<br>                                                                  // Horizontally centered (the position is very important, it is recommended to be at the initial position) <br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>            $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);<br>                                                             $objActSheet->setCellValue('A1', 'Item number');<br>            $objActSheet->setCellValue('B1', 'Product Name');<br>            $objActSheet->setCellValue('C1', 'Product Image');<br>            $objActSheet->setCellValue('D1', 'Product barcode');<br>            $objActSheet->setCellValue('E1', 'Product Attributes');<br>           $objActSheet->setCellValue('F1', 'Quote (Hong Kong Dollar)');<br> ​​​​//Set a table width<br>             $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);<br>            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);<br>            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);<br>            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);<br>            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);<br>        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);<br>         <br>         // 垂直居中<br>         $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);<br>         $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);<br>         $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);<br>         $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);<br>         $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);<br>         <br>         foreach($data as $k=>$v){<br>             $k  =2;<br>             $objActSheet->setCellValue('A'.$k, $v['goods_sn']);    <br>             $objActSheet->setCellValue('B'.$k, $v['goods_name']);    <br>                 <br>             <br>             $img = M('goods')->where('goods_id = '.$v['goods_id'])->field('goods_thumb')->find();<br>             // 图片生成<br>             $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();<br>             $objDrawing[$k]->setPath('./Upload/'.$img['goods_thumb']);<br>             // 设置宽度高度<br>             $objDrawing[$k]->setHeight(80);//照片高度<br>             $objDrawing[$k]->setWidth(80); //照片宽度<br>             /*设置图片要插入的单元格*/<br>             $objDrawing[$k]->setCoordinates('C'.$k);<br>             // 图片偏移距离<br>             $objDrawing[$k]->setOffsetX(12);<br>             $objDrawing[$k]->setOffsetY(12);<br>             $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());<br>             <br>             // 表格内容<br>             $objActSheet->setCellValue('D'.$k, $v['barcode']);    <br>             $objActSheet->setCellValue('E'.$k, $v['goods_type']);    <br>             $objActSheet->setCellValue('F'.$k, $v['price']);<br>                 <br>             // 表格高度<br>             $objActSheet->getRowDimension($k)->setRowHeight(80);<br>                                                                              }<br>                                                           $fileName = 'Quotation Form';<br> $date = date("Y-m-d",time());<br>           $fileName .= "_{$date}.xls";<br> <br>         $fileName = iconv("utf-8", "gb2312", $fileName);<br> ​​​​ //Rename table<br>                   // $objPHPExcel->getActiveSheet()->setTitle('test');<br> ​​​​ //Set the active order index to the first table, so this is the first table when Excel opens<br>            $objPHPExcel->setActiveSheetIndex(0);<br> header('Content-Type: application/vnd.ms-excel');<br> header("Content-Disposition: attachment;filename="$fileName"");<br>          header('Cache-Control: max-age=0');<br> <br>          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');<br>            $objWriter->save('php://output'); //The file is downloaded through the browser<br>                       //                                                         }<br>Notes:<br> 1.phpexcel download address: http://phpexcel.codeplex.com/<br> 2. Put the files in the Classes directory (PHPExcel.php and PHPExcel folder) into the ThinkPHPLibraryOrgUtil directory (it can also be placed in other directories, as long as the class can be loaded, there will be no problem). And change PHPExcel.php to PHPExcel.class.php (this step is very important, the php file you need to load must be changed to xxx.class.php)

3. Manually import classes (please note the "" symbol)
import("Org.Util.PHPExcel");
$objPHPExcel = new PHPExcel();
(I have tried to load classes automatically, but it is more troublesome, because phpexcel files need to add "namespace OrgUtil;", so I simply choose to load classes manually)

4. Horizontal centering, vertical centering, height and other settings. Pay attention to the position. If you put it at the end, the following will take effect. Put it in the head, and the first line will take effect (the above code will take effect on the first line, if it is placed in foreach, the next line will take effect).

5. Import introduction. import("Org.Util.PHPExcel.Writer.Excel2007"); File location: OrgUtilPHPExcelWriterExcel2007.class.php

6. The image address must be local.
$objDrawing[$k]->setPath('./Upload/'.$img['goods_thumb']); Image location: Installation directory/Upload/xxx

Common problems:
1.'class xxx not found' If the class import is unsuccessful, you can first put a function ss(){} in xxx.class.php and then output $xx = new xxx; $xx->ss() to see the result. .

.....

Thoughts: When I first started doing this, I was very confused because I had never done this before and had no experience. Then, Du Niang, Du Niang,... Then use phpexcel to generate a simple text output; then generate a simple picture output, and finally combine the questions and pictures to output, and you will have the current example code. First a small simple step, then a small step, and finally a big step, and it becomes

thinkphp3.2 and phpexcel import
http://www.thinkphp.cn/code/2124.html
Yunqi Conference Beijing Station: It is rare for Alibaba technical experts to appear on the scene. More than 100 people came this time? !

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn