• composer 安装 phpoffice/phpspreadsheet
// 引入 namespace
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// 创建
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// 设置文件名
$fileName = 'cnbattle.xlsx';

// 设置表格数据,当前为循环设置 1000行,25列的数据
$hang = 1000;
$lei = 26;
for ($x = 0; $x < $lei; $x++) {
    if ($lei>26){
        die('$lei不能大于26');
    }
    $leiStr = chr(65 + $x);
    for ($i = 1; $i <= $hang; $i++) {
        $sheet->setCellValue($leiStr . $i, 'value-' . $leiStr . $i);
    }
}
//告诉浏览器输出07Excel文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//告诉浏览器将要输出Excel03版本文件
//header(‘Content-Type:application/vnd.ms-excel‘);
//告诉浏览器输出文件名称
header('Content-Disposition: attachment;filename="'.$fileName.'"');
//禁止缓存header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
来源:中战博客,欢迎分享!
标签: php,composer,phpspreadsheet,excel