PhpSpreadsheet 导出 Excel 文件 demo

  1. 使用 composer 安装 PhpSpreadsheet
composer require phpoffice/phpspreadsheet
  1. Demo
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// 告诉浏览器输出07Excel文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// 告诉浏览器输出浏览器名称
header('Content-Disposition: attachment;filename="'.$fileName.'"');
// 禁止缓存
header('Cache-Control: max-age=0');

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet()->setTitle('sheet1');
foreach ($cellData as $row => $cols) {
    $row += 1;
    foreach ($cols as $col => $cellValue) {
        $col += 1;
        $sheet->setCellValueByColumnAndRow($col, $row, $cellValue);
    }
}
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');

你可能感兴趣的:(PhpSpreadsheet 导出 Excel 文件 demo)