>  기사  >  php教程  >  thinkphp3.2和phpexcel导出带生成图片完美案例

thinkphp3.2和phpexcel导出带生成图片完美案例

WBOY
WBOY원래의
2016-07-06 13:29:211236검색

thinkphp3.2和phpexcel导出带生成图片完美案例
新手一枚,接触tp也有一个月了,最近公司要求报表导出要有图片生成,特意花时间研究了下,看事例代码// 导出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>         <br>         // 导出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>         <br>         $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);<br>     <br>         $objActSheet = $objPHPExcel->getActiveSheet();<br>         <br>         // 水平居中(位置很重要,建议在最初始位置)<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>         <br>         $objActSheet->setCellValue('A1', '商品货号');<br>         $objActSheet->setCellValue('B1', '商品名称');<br>         $objActSheet->setCellValue('C1', '商品图');<br>         $objActSheet->setCellValue('D1', '商品条码');<br>         $objActSheet->setCellValue('E1', '商品属性');<br>         $objActSheet->setCellValue('F1', '报价(港币)');<br>         // 设置个表格宽度<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>         }<br>         <br>         $fileName = '报价表';<br>         $date = date("Y-m-d",time());<br>         $fileName .= "_{$date}.xls";<br> <br>         $fileName = iconv("utf-8", "gb2312", $fileName);<br>         //重命名表<br>         // $objPHPExcel->getActiveSheet()->setTitle('test');<br>         //设置活动单指数到第一个表,所以Excel打开这是第一个表<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'); //文件通过浏览器下载<br>         // END    <br>     }注意事项:
1.phpexcel下载地址:http://phpexcel.codeplex.com/

2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件夹),放到ThinkPHP\Library\Org\Util目录下(放到其他目录下也可以,只需要能加载类都没有问题)。并把PHPExcel.php 改成PHPExcel.class.php(这一步很重要,你需要加载的php文件都要改成xxx.class.php才行)

3.手动导入类(请注意"\"符号)
import("Org.Util.PHPExcel");
$objPHPExcel = new \PHPExcel();
(本人试过自动加载类,比较麻烦,因为phpexcel的文件都需要加上“namespace Org\Util;”,所以干脆就选择手动加载类)

4.水平居中,垂直居中,高度 等设置。注意放的位置,如果你放在末尾,那么是下一样生效。放到头部,第一行生效(上面代码是第一行生效,如果放到foreach里面就是下一行生效)。

5.import介绍。import("Org.Util.PHPExcel.Writer.Excel2007"); 文件位置:Org\Util\PHPExcel\Writer\Excel2007.class.php

6.图片地址一定要是本地。
$objDrawing[$k]->setPath('./Upload/'.$img['goods_thumb']); 图片位置:安装目录/Upload/xxx

容易出现的问题:
1.'class xxx not found' 类导入不成功,可以先在xxx.class.php 放个function ss(){} 然后输出下$xx = new xxx; $xx->ss()输出下看结果。

.....

心得:刚开始做这个的时候,头非常的大,因为压根没做过这个,也没经验。然后,度娘、度娘、... 再用phpexcel先生成一个简单的文本输出;再生成一个简单的图片输出,最后问题和图片结合输出,就有了现在的实例代码。先简单一小步,再一小步,最后一大步,就成了

thinkphp3.2和phpexcel导入
http://www.thinkphp.cn/code/2124.html

云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.