常用

具体可参考phpoffice官方示例vendor/phpoffice/phpspreadsheet/samples/Basic/05_Feature_demo.php

  1. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  2. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  3. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  4. $spreadsheet = new Spreadsheet();
  5. $sheet = $spreadsheet->getActiveSheet();
  6. //设置单元格值
  7. $sheet->setCellValue('A1', $title);
  8. //设置单元格样式,加粗,字体大小20
  9. $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setBold(true)->setSize(20.0);
  10. //合并单元格
  11. $spreadsheet->getActiveSheet()->mergeCells('A1:H1');
  12. //设置合并后的单元格水平居中对齐
  13. $spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  14. //报错xlsx
  15. $writer = new Xlsx($spreadsheet);
  16. $filename = '文件名.xlsx';
  17. $tempnam = tempnam(sys_get_temp_dir(),'prefix');
  18. $writer->save($tempnam);
  19. //thinkphp5.1下载
  20. return download($tempnam,$filename);