PHP 导出Excel

it2026-10-03  2

use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; $file_name = '数据.xls'; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->mergeCells('A1:A3'); //合并 A1:A3 //居中样式 vertical $style_array = [ 'alignment' => [ 'horizontal' =>Alignment::HORIZONTAL_CENTER, //水平居中 'vertical' => Alignment::VERTICAL_CENTER] //垂直居中 ]; $sheet->getStyle('A1')->applyFromArray($style_array); //设置样式 $sheet->setCellValue('A1', '事业部'); 设置值 //合并 B1:C1 $sheet->mergeCells("B1:C1"); //设置字体加粗 字体颜色 $sheet->getStyle('A1')->getFont()->setBold(true)->getColor()->setRGB('00BFFF');; //设置列宽 $sheet->getColumnDimension('A')->setWidth(12); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename='.$file_name); header('Cache-Control: max-age=0'); header('Access-Control-Allow-Origin:*'); //跨域设置 $writer = IOFactory::createWriter($spreadsheet, 'Xls'); $writer->save('php://output'); return true;

 

最新回复(0)