thinkphp5中引入第三方扩展的方法extend
玩的还是phpexcel
目录里面的文件夹是
使用方法
1 控制器顶部 加入
use thinkLoader;
2 控制器的函数里面
Loader::import("phpexcel.PHPExcel");
整个文件代码
<?php namespace appadmincontroller; // use app hinkController; use appcommoncontrollerBackend; use appadminmodelSecurityCode as SecurityCodeModel; use thinkRequest; use thinkLoader; class SecurityCode extends Backend { //导出 public function Output() { $cc = new SecurityCodeModel(); $info = $cc->order('id')->select(); $data=array(); foreach($info as $k=>$v) { $data[$k]['id']=$v['id']; $data[$k]['serial_number']=$v['serial_number']; // $data[$k]['code1']=$v['code1']; // $data[$k]['code2']=$v['code2']; // $data[$k]['is_active']=$v['is_active']; // $data[$k]['product_code']=$v['product_code']; // $data[$k]['line']=$v['line']; // $data[$k]['xuhao']=$v['xuhao']; $data[$k]['batch']=$v['batch']; } $filename='序列号'; $field = array( 'A' => array('id', '序号'), 'B' => array('serial_number', '序列号'), 'C' => array('batch', '批次'), ); //$this->downloadExcel($data,$title,$filename); $this->phpExcelList($field,$data,$filename); } /** * 直接导出需要生产的内容 * @param $field * @param $list * @param string $title * @throws PHPExcel_Exception * @throws PHPExcel_Writer_Exception */ public function phpExcelList($field, $list, $filename='文件') { //vendor("PHPExcel.PHPExcel"); Loader::import("phpexcel.PHPExcel");//引入phpexcel文件夹下的PHPExcel $objPHPExcel = new PHPExcel(); $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); //设置保存版本格式 foreach ($list as $key => $value) { foreach ($field as $k => $v) { if ($key == 0) { $objPHPExcel->getActiveSheet()->setCellValue($k . '1', $v[1]); } $i = $key + 2; //表格是从2开始的 $objPHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]); } } header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename='.$filename.'.xls'); header("Content-Transfer-Encoding:binary"); $objWriter->save('php://output'); } }