phpexcel生成excel文件:
一、安裝phpexcel:
composer require phpoffice/phpexcel
二、phpexcel主要用到的類:
phpExcel和PHPexcel_IOFactory
三、phpexcel使得使用方法:
方法a:在index.php中直接使用:
1、引入phpExcel和PHPexcel_IOFactory類:
use PHPExcel; use PHPExcel_IOFactory;
2、在vendor->phpoffice->Example中瀏覽案列:
3、在index.php中寫入方法:
public function testExcel(){ // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); // Miscellaneous glyphs, UTF-8 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A4', 'Miscellaneous glyphs') ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (OpenDocument) header('Content-Type: application/vnd.oasis.opendocument.spreadsheet'); header('Content-Disposition: attachment;filename="01simple.ods"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'OpenDocument'); $objWriter->save('php://output'); exit; }
4、在瀏覽器中訪問testexcel方法
方法b、在擴展配置中使用phpexcel:
1、新建Excel.php配置文件
extend->Excel->Excel.php
2、給定命名空間和配置類
namespace Excel; use PHPExcel; use PHPExcel_IOFactory;
3、定義方法
public static function export($data,$title){ ini_set('max_execution_time', '0');//最長執行時間,php默認爲30秒,這裏設置爲0秒的意思是保持等待知道程序執行完成 $phpexcel = new PHPExcel(); // Set properties 設置文件屬性 $properties = $phpexcel->getProperties(); $properties->setCreator("Boge");//作者是誰 可以不設置 $properties->setLastModifiedBy("Boge");//最後一次修改的作者 $properties->setTitle($title);//設置標題 $properties->setSubject('測試');//設置主題 $properties->setDescription("備註");//設置備註 $properties->setKeywords("關鍵詞");//設置關鍵詞 $properties->setCategory("類別");//設置類別 $sheet = $phpexcel->getActiveSheet(); $sheet->fromArray($data); $sheet->setTitle('Sheet1');//設置sheet名稱 /**---------- 簡單寫入 ----------**/ // $sheet ->setCellValue("A1","問卷標題"); //可以指定單元格位置 // $sheet ->setCellValue("A2","今天你吃了嗎?"); //可以指定單元格位置 // public function test1(){ // $data = array(); // Excel::export($data,'test'); // } //// /**---------- 遍歷寫入數據 --------------**/ // // 準備數據 // $data = array( // 按照該結構封裝即可 // 'cols' => array('姓名','班級','年齡'), // 'rows' =>array( // array('小明','三年一班','10歲'), // array('小波','三年二班','30歲'), // array('小薛','三年三班','11歲'), // ), // ); //// //// // 提前定義好 列號映射數組 // $colArray = array('A','B','C'); // 如果列數大於 Z ,使用 AA-AZ BA-BZ ... ZA-ZZ ; //// //// // 遍歷表頭 // foreach ($data['cols'] as $key => $col) { // $sheet ->setCellValue($colArray[$key].'1' , $col); // } //// //// // 遍歷數據 // foreach ($data['rows'] as $key => $row) { // $rowNum = $key+2; // 數據從第二行開始寫,第一行是表頭 // foreach ($row as $key => $value) { // $sheet ->setCellValue($colArray[$key].$rowNum , $value); // } // } $phpexcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=".$title.".xls"); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5'); $objwriter->save('php://output'); exit; }
4、在index.php中調用方法
use Excel\Excel;
public function test1(){ $data = array( array('名稱', '價格', '數量', '大小','公司'), array('Q1', 666, 15, 21,'baidu'), array('Q2', 555, 73, 86,'ali'), array('Q3', 1111, 61, 69,'yzm'), array('Q4', 30, 32, 0,'taobao'), ); Excel::export($data,'test'); }
方法3、方法和方法二略同
1、封裝一個方法:
public static function exportexcel($data,$title){ ini_set('max_execution_time', '0');//最長執行時間,php默認爲30秒,這裏設置爲0秒的意思是保持等待知道程序執行完成 $phpexcel = new PHPExcel(); // Set properties 設置文件屬性 $properties = $phpexcel->getProperties(); $properties->setCreator("Boge");//作者是誰 可以不設置 $properties->setLastModifiedBy("Boge");//最後一次修改的作者 $properties->setTitle($title);//設置標題 $properties->setSubject('測試');//設置主題 $properties->setDescription("備註");//設置備註 $properties->setKeywords("關鍵詞");//設置關鍵詞 $properties->setCategory("類別");//設置類別 $sheet = $phpexcel->getActiveSheet(); $sheet->setTitle('Sheet1');//設置sheet名稱 //從A開始 $startLetter = 'A'; $rowNumber = 1; // 遍歷表頭 foreach ($data['cols'] as $key => $col) { $sheet ->setCellValue($startLetter++.$rowNumber , $col); } ++$rowNumber; // 遍歷數據 foreach ($data['rows'] as $key => $row) { $startLetter = 'A'; foreach ($row as $key => $value) { $sheet ->setCellValue($startLetter++.$rowNumber , $value); } ++ $rowNumber; } $phpexcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=".$title.".xls"); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5'); $objwriter->save('php://output'); exit; }
2、在index.php中寫入方法
public function exportexcel(){ //準備數據 $data = array( 'cols' => array('姓名','班級','年齡'), 'rows' => array( array('小明','三年一班','10歲'), array('小明','三年一班','10歲'), array('小明','三年一班','10歲'), array('小明','三年一班','10歲'), array('小明','三年一班','10歲'), ), ); Excel::exportexcel($data,'數據統計報表'.date('Y-m-d',time())); }