美文网首页
php-xlswriter 使用

php-xlswriter 使用

作者: AGEGG | 来源:发表于2021-05-11 17:12 被阅读0次

导大表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

<?php

namespace app\common\library;


class XlsWriter
{

    //服务器缓存地址
    const TEMP_PATH = RUNTIME_PATH.'excel';

    private $fileObject;
    private $excelName;


    /**
     * 创建一个excel
     * @param $paramsHeader
     * @param $paramsData
     * @param string $sheetName
     * @param string $excelName
     */
    public function createExcel($paramsHeader, $paramsData, $sheetName='Sheet1',$excelName= '')
    {
        $this->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 使用

      本文链接:https://www.haomeiwen.com/subject/itkndltx.html