当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp3.2中实现phpexcel导出带生成图片示例

发布:smiling 来源: PHP粉丝网  添加日期:2018-08-02 09:37:19 浏览: 评论:0 

首先下载PHPEXCEL  下载地址:http://phpexcel.codeplex.com/        https://github.com/PHPOffice/PHPExcel

把Classes目录下的文件(PHPExcel.php和PHPExcel文件夹),放到ThinkPHP\Library\Org\Util目录下

PHPExcel.php  改名为 :PHPExcel.class.php

  1. // 导出exl 
  2.   
  3.   publicfunctionlook_down(){ 
  4.   
  5.     $id= I('get.id'); 
  6.   
  7.     $m= M ('offer_goods'); 
  8.   
  9.     $where['offer_id'] =$id
  10.   
  11.     $data=$m->field('goods_id,goods_sn,goods_name,barcode,goods_type,price')-><a href="/tags.php/select/" target="_blank">select</a>(); 
  12.   
  13.        
  14.   
  15.     // 导出Exl 
  16.   
  17.     import("Org.Util.PHPExcel"); 
  18.   
  19.     import("Org.Util.PHPExcel.Worksheet.Drawing"); 
  20.   
  21.     import("Org.Util.PHPExcel.Writer.Excel2007"); 
  22.   
  23.     $objPHPExcel=new\PHPExcel(); 
  24.   
  25.        
  26.   
  27.     $objWriter=new\PHPExcel_Writer_Excel2007($objPHPExcel); 
  28.   
  29.      
  30.   
  31.     $objActSheet=$objPHPExcel->getActiveSheet(); 
  32.   
  33.        
  34.   
  35.     // 水平居中(位置很重要,建议在最初始位置) 
  36.   
  37.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  38.   
  39.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  40.   
  41.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  42.   
  43.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  44.   
  45.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  46.   
  47.     $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  48.   
  49.        
  50.   
  51.     $objActSheet->setCellValue('A1','商品货号'); 
  52.   
  53.     $objActSheet->setCellValue('B1','商品名称'); 
  54.   
  55.     $objActSheet->setCellValue('C1','商品图'); 
  56.   
  57.     $objActSheet->setCellValue('D1','商品条码'); 
  58.   
  59.     $objActSheet->setCellValue('E1','商品属性'); 
  60.   
  61.     $objActSheet->setCellValue('F1','报价(港币)'); 
  62.   
  63.     // 设置个表格宽度 
  64.   
  65.     $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16); 
  66.   
  67.     $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80); 
  68.   
  69.     $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); 
  70.   
  71.     $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); 
  72.   
  73.     $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12); 
  74.   
  75.     $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12); 
  76.   
  77.        
  78.   
  79.     // 垂直居中 
  80.   
  81.     $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  82.   
  83.     $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  84.   
  85.     $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  86.   
  87.     $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  88.   
  89.     $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  90.   
  91.        
  92.   
  93.     <a href="/tags.php/foreach/" target="_blank">foreach</a>($dataas$k=>$v){ 
  94.   
  95.       $k+=2; 
  96.   
  97.       $objActSheet->setCellValue('A'.$k,$v['goods_sn']);  
  98.   
  99.       $objActSheet->setCellValue('B'.$k,$v['goods_name']);  
  100.   
  101.            
  102.   
  103.          
  104.   
  105.       $img= M('goods')->where('goods_id = '.$v['goods_id'])->field('goods_thumb')->find(); 
  106.   
  107.       // 图片生成 
  108.   
  109.       $objDrawing[$k] =new\PHPExcel_Worksheet_Drawing(); 
  110.   
  111.       $objDrawing[$k]->setPath('./Upload/'.$img['goods_thumb']); 
  112.   
  113.       // 设置宽度高度 
  114.   
  115.       $objDrawing[$k]->setHeight(80);//照片高度 
  116.   
  117.       $objDrawing[$k]->setWidth(80);//照片宽度 
  118.   
  119.       /*设置图片要插入的单元格*/ 
  120.   
  121.       $objDrawing[$k]->setCoordinates('C'.$k); 
  122.   
  123.       // 图片偏移距离 
  124.   
  125.       $objDrawing[$k]->setOffsetX(12); 
  126.   
  127.       $objDrawing[$k]->setOffsetY(12); 
  128.   
  129.       $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet()); 
  130.   
  131.          
  132.   
  133.       // 表格内容 
  134.   
  135.       $objActSheet->setCellValue('D'.$k,$v['barcode']);  
  136.   
  137.       $objActSheet->setCellValue('E'.$k,$v['goods_type']);  
  138.   
  139.       $objActSheet->setCellValue('F'.$k,$v['price']); 
  140.   
  141.            
  142.   
  143.       // 表格高度 
  144.   
  145.       $objActSheet->getRowDimension($k)->setRowHeight(80); 
  146.   
  147.          
  148.   
  149.     } 
  150.   
  151.        
  152.   
  153.     $fileName='报价表' 
  154.   
  155.     $date=date("Y-m-d",time()); 
  156.   
  157.     $fileName.="_{$date}.xls"
  158.   
  159.     $fileName= <a href="/tags.php/iconv/" target="_blank">iconv</a>("utf-8","gb2312",$fileName); 
  160.   
  161.     //重命名表 
  162.   
  163.     // $objPHPExcel->getActiveSheet()->setTitle('test'); 
  164.   
  165.     //设置活动单指数到第一个表,所以Excel打开这是第一个表 
  166.   
  167.     $objPHPExcel->setActiveSheetIndex(0); 
  168.   
  169.     header('Content-Type: application/vnd.ms-excel'); 
  170.   
  171.     header("Content-Disposition: attachment;filename=\"$fileName\""); 
  172.   
  173.     header('Cache-Control: max-age=0'); 
  174.   
  175.     $objWriter= \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5'); 
  176.     //phpfensi.com 
  177.     $objWriter->save('php://output'); //文件通过浏览器下载 
  178.   
  179.     // END  
  180.   
  181.   } 

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

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

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

Tags: thinkphp3 2 phpexcel

分享到: