php-xlswriter 使用

导大表excel时使用PhpSpreadsheet会占用大量内存,
而使用php-xlswriter 可以降低内存使用峰值。

文档如下:https://xlswriter-docs.viest.me/zh-cn/nei-cun/gu-ding-nei-cun-mo-shi

windows上的安装

image.png

image.png

注意几个位置:因此应该选 7.3 NTS x64的版本
extension=php_xlswriter.dll

使用

XlsWriter.php

excelName = $excelName ? $excelName : '表格'.time();
        $tempPath = self::TEMP_PATH;
        if (!is_dir(dirname($tempPath))) {
            mkdir(dirname($tempPath), 0777, true);
        }
        $config = [
            'path' => $tempPath
        ];

        $excel = new \Vtiful\Kernel\Excel($config);

        $this->fileObject = $excel->constMemory("{$this->excelName}.xlsx",$sheetName);

        $fileHandle = $this->fileObject->getHandle();
        $format    = new \Vtiful\Kernel\Format($fileHandle);
        $boldStyle = $format->bold()->toResource();

        $this->fileObject->header($paramsHeader)
            ->data($paramsData);

    }


    /**
     * 向文件中追加一个工作表
     * @param $paramsHeader
     * @param $paramsData
     * @param string $sheetName
     */
    public function addExcel($paramsHeader, $paramsData, $sheetName='Sheet2')
    {
        $this->fileObject->addSheet($sheetName)
            ->header($paramsHeader)
            ->data($paramsData);
    }


    /**
     * 输出excel
     */
    public function outputAll()
    {
        $filePath = $this->fileObject->output();
        //@unlink($filePath);
    }
}

你可能感兴趣的:(php-xlswriter 使用)