首先用到 PHPexcel,官網(wǎng)地址 :https://packagist.org/packages/phpoffice/phpexcel
安裝方法:composer require phpoffice/phpexcel
[root@xxxx]# composer require phpoffice/phpexcel
Warning: This development build of composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Using version ^1.8 for phpoffice/phpexcel
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing phpoffice/phpexcel (1.8.1)
Downloading: 100%
Writing lock file
Generating autoload files
安裝完成。。。。
引用:
use PHPExcel;
use PHPExcel_IOFactory;
實例化...
public function export(){
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("文件作者信息...")
->setLastModifiedBy("文件最后修改作者")
->setTitle("文件標(biāo)題")
->setSubject("這是啥啊")
->setDescription("文件描述")
->setKeywords("文件關(guān)鍵詞 貌似是用空格隔開")
->setCategory("Test result file");
// 添加第一欄數(shù)據(jù)
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '優(yōu)惠券代碼')
->setCellValue('B1', '優(yōu)惠券規(guī)則組')
->setCellValue('C1', '結(jié)束時間')
->setCellValue('D1', '使用規(guī)則')
->setCellValue('E1', '綁定ID(課程或系列)')
->setCellValue('F1', '使用范圍');
//二維數(shù)組 從數(shù)據(jù)庫拿出來
$excelData = D('coupons')->limit(10)->select();
for ($i=2;$i<count($excelData);$i++){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $excelData[$i]['code'])
->setCellValue('B'.$i, $excelData[$i]['group'])
->setCellValue('C'.$i, $excelData[$i]['indate'])
->setCellValue('D'.$i, $excelData[$i]['rule'])
->setCellValue('E'.$i, $excelData[$i]['bind'])
->setCellValue('F'.$i, $excelData[$i]['useRange']);
}
// 設(shè)置下邊的文章標(biāo)題 我也不知道叫啥
$objPHPExcel->getActiveSheet()->setTitle('就一頁');
// 設(shè)置打開文件瀏覽的頁面(0為第一頁)
$objPHPExcel->setActiveSheetIndex(0);
// 保存為Excel 2007 文件
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', '優(yōu)惠券'));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="文件名稱.xlsx"');
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, 'Excel2007');
$objWriter->save('php://output');
}
我就用到這么多,等我用到其他的時候再來更新吧!