chinahub/xls-writer

基于xlsWriter的php xls库

v1.2.2 2022-04-14 09:29 UTC

This package is auto-updated.

Last update: 2024-09-14 14:40:03 UTC


README

基于xlsWriter的php xls库,php-xlsWriter:https://github.com/viest/php-ext-xlswriter

安装

运行以下命令以安装包的最新适用版本:

composer require chinahub/xls-writer

环境需求

  • xlswriter 扩展
pecl install xlswriter
# add extension = xlswriter.so to php.ini
  • 推荐 PHP > 7.4

使用方法

导出

use Chinahub\XlsWriter\interfaces\ExportInterface;

class UserExport implements ExportInterface
{
    public function headers(): array
    {
        return ['id','name','email'];
    }

    public function data(): array
    {
        return [
            [1,'tom','test@qq.com'],
            [2,'lily','test@gmail.com'],
            [3,'lisa','test@163.com'],

        ];
    }
}

输出路径

use Chinahub\XlsWriter\Export;

$excel = new Export(new UserExport());
$excel->config = ['path' => '/www'];
$excel->fileName = 'user.xlsx';
$excel->output();

输出下载

use Chinahub\XlsWriter\Export;

$excel = new Export(new UserExport());
$excel->fileName = 'user.xlsx';
$excel->download();

导入

从工作表获取所有数据

use Chinahub\XlsWriter\Import;

$excel = new Import('/www/user.xlsx');
$excel->getSheet();

从工作表获取行

use Chinahub\XlsWriter\Import;

$excel = new Import('/www/user.xlsx');
$excel = $excel->instance();
while (($row = $excel->nextRow()) !== NULL) {
    var_dump($row);
}