Using PHPExcel in thinkphp5.0+

1. Install PHPExcel

1. The TP5 version can be installed directly using composer (composer will not find the tutorial by yourself):
composer require phpoffice/phpexcel

Very convenient.

2. Download PHPExcel from the official website, and then put it in the vendor directory or in the extend directory (the following is to put it in the extend directory)

2. Introduce the PHPExcel class into the control page

EXTEND_PATH. The path to PHPExcel.php in your PHPExcel


1. Generate an excel instance


2. Export an excel sheet and save it to the local


3. Download data from database to generate excel

1. Encapsulate database data


2. Encapsulate the excel download function


3. Test


Enter your test address in the browser and it will pop up


download successful

Fourth, the overall code

<?php
namespace app\home\controller;
require_once EXTEND_PATH.'PHPExcel/Classes/PHPExcel.php';
class Index
{
	/**
 	* Generate an excel file
 	*/
 	public function index(){
 		$path=dirname(__FILE__); //The directory where the current script is located
 		$PHPExcel=new \PHPExcel(); //Instantiate the PHPExcel class, similar to in Create a new Excel sheet on the desktop
 		$PHPSheet= $PHPExcel->getActiveSheet(); //Get the operation object of the current active sheet
 		$PHPSheet->setTitle('demo'); //Set the name to the current active sheet
 		$PHPSheet->setCellValue ('A1','name')->setCellValue('B1','score'); //Fill data for the current active sheet, the data is filled line by line in order, if you want to leave A1 blank, you can directly setCellValue('A1','');
 		$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel5'); //Generate Excel file according to the specified format, 'Excel2005' means to generate 2005 version of xlsx,
 		$PHPWriter-> save($path.'/demo1.xlsx'); //Indicates that demo1 is generated under the $path path.
xlsx file 	}
 /**
 * Export an excel and save it in the local file directory
 */
 public function excelDownload(){
	
						$path=dirname(__FILE__); //The directory where the current script is located
 		$PHPExcel=new \PHPExcel(); //Instantiate the PHPExcel class, similar to creating a new Excel sheet on the desktop
 		$PHPSheet= $PHPExcel->getActiveSheet() ; //Get the operation object of the current active sheet
 		$PHPSheet->setTitle('demo'); //Set the name to the current active sheet
 		$PHPSheet->setCellValue('A1','name')->setCellValue('B1' ,'score'); //Fill data for the current active sheet. The data is filled line by line in sequence. If you want to leave A1 blank, you can directly setCellValue('A1','');
 		$PHPWriter = \PHPExcel_IOFactory: :createWriter($PHPExcel,'Excel5'); //Generate an Excel file according to the specified format,
 		// Redirect output to a client's web browser (Excel5)
 		header('Content-Type: application/vnd.ms-excel'); / / Tell the browser to generate an excel05 version of the table
 		header('Content-Disposition: attachment;filename="01simple.xls"'); // Tell the browser the name of the output file
 		header('Cache-Control:max-age=0'); //Disable caching
        $PHPWriter->save("php://output"); //Output to browser
		
	}


	/**
 	* Test downloading data from database
 	*/
 	public function test(){
 		dump($this->outExceDatas());
 	}
	/**
	 * 封装excel下载
	 * @param  array  $field 
	 * @param  array  $list  
	 * @param  string $title 
	 * @return [type]   
	 */
	public function phpExcelList($field=[],$list=[],$title='文件'){
		$PHPExcel=new \PHPExcel();
		$PHPSheet=$PHPExcel->getActiveSheet();
		$PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
		foreach($list as $key=>$value)
		{
			foreach($field as $k=>$v){
				if($key == 0){
					$PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
				}
				$i=$key+2; 
				$PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
}
}
				
							$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel5'); //Generate an Excel file according to the specified format,
 		// Redirect output to a client's web browser (Excel5)
 		header('Content-Type: application/vnd.ms -excel'); // Tell the browser to generate an excel05 version of the table
 		header("Content-Disposition: attachment;filename={$title}.xls"); // Tell the browser the name of the output file
 		header('Cache- Control: max-age=0'); //Disable caching
        $PHPWriter->save("php://output"); //Output to browser
	}
 	/**
 	* Encapsulate database data
 	*/
 	public function outExceDatas(){
 		//Replace data in database with array
 		$arr=[
 			0=>[
 				'price'=>200,
 				'uname'=>'user1',
 			] ,
 			1=>[
 				'price'=>300,
 				'uname'=>'user2',
 			],
 			2=>[
 				'price'=>400,
 				'uname'=>'user3',
 			],
 			3=>[
 				' price'=>500,
 				'uname'=>'user4',
 			],
 		];
 		foreach($arr as $key => $value){
 			$arr[$key]['price']=number_format($value[' price'],2);
 		}
 		$field=array(
			
            'A' => array('uname', 'username'),
            'B' => array('price', 'amount (yuan)'),
          
		);
 		$until=$this->phpExcelList($field,$arr,'recharge list'); }		
	
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324648209&siteId=291194637