Excel

phpoffice/phpspreadsheet

项目地址

https://github.com/PHPOffice/PhpSpreadsheet

安装

  composer require phpoffice/phpspreadsheet

使用

<?php
namespace app\controller;

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

class ExcelController
{
    public function index($request)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Hello World !');

        $writer = new Xlsx($spreadsheet);
        $file_path = public_path().'/hello_world.xlsx';
        // 保存文件到 public 下
        $writer->save($file_path);
        // 下载文件
        return response()->download($file_path, '文件名.xlsx');
    }

}

更多内容

访问 https://phpspreadsheet.readthedocs.io/en/latest/

编辑于2024-03-13 17:42:49 完善本页 +发起讨论
赞助商
×