Yêu cầu thg 4 23, 2019 3:31 SA 72 0 0
  • 72 0 0
0

Maatwebsite báo lỗi Xlsx::readColumnsAndRowsAttributes() must be an instance of SimpleXMLElement, boolean given

Chia sẻ
  • 72 0 0

Mình có dùng bản Laravel Maatwebsite 3.1 xuất ở local thì bình thường nhưng khi đẩy lên server thì lại báo lỗi này

Argument 1 passed to PhpOffice\PhpSpreadsheet\Reader\Xlsx::readColumnsAndRowsAttributes() must be an instance of SimpleXMLElement, boolean given

Code export của mình đây

<?php

namespace App\Exports;

use App\Models\Txn;
use App\Models\WalletUser;
use Illuminate\Queue\SerializesModels;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithEvents;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class TxnsExport implements FromQuery, ShouldQueue, WithHeadings, WithMapping, WithEvents, ShouldAutoSize, WithColumnFormatting
{
	use Exportable, SerializesModels;

	public $request;
    const NUMBER_FORMAT = '#,#0;-#,#0';

	public function __construct($request)
	{
        $this->request = $request;
	}

	/**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
            	$styleArray = [
				    'font' => [
				        'bold' => true,
				    ],
				    'alignment' => [
				        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
				        'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
				    ],
				];
                $cellRange = 'A1:W1'; // All headers
                $event->sheet->getDelegate()
			                ->getStyle($cellRange)
			                ->applyFromArray($styleArray);

                $highestRow = $event->sheet->getDelegate()->getHighestRow();
                $styleArray = [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                    ],
                ];
                $event->sheet->getDelegate()
                            ->getStyle('A2:A'.$highestRow)
                            ->applyFromArray($styleArray);
                $event->sheet->getDelegate()
                            ->getStyle('B2:B'.$highestRow)
                            ->applyFromArray($styleArray);
                $event->sheet->getDelegate()
                            ->getStyle('H2:H'.$highestRow)
                            ->applyFromArray($styleArray);
                $event->sheet->getDelegate()
                            ->getStyle('I2:I'.$highestRow)
                            ->applyFromArray($styleArray);
                $event->sheet->getDelegate()
                            ->getStyle('J2:J'.$highestRow)
                            ->applyFromArray($styleArray);
                $event->sheet->getDelegate()
                            ->getStyle('K2:K'.$highestRow)
                            ->applyFromArray($styleArray);
            },
        ];
    }

    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_NUMBER,
            'B' => NumberFormat::FORMAT_NUMBER,
            'H' => self::NUMBER_FORMAT,
            'I' => self::NUMBER_FORMAT,
            'J' => self::NUMBER_FORMAT,
            'K' => self::NUMBER_FORMAT,
        ];
    }

	public function headings(): array
    {
        return [
            'Mã người dùng',
            'Mã giao dịch',
            'Loại giao dịch',
            'Trạng thái',
            'Từ/Đến',
            'Số tham chiếu',
            'Số tham chiếu ngân hàng',
            'Số dư đầu',
            'Số tiền',
            'Phí',
            'Số dư cuối',
            'Thời gian'
        ];
    }

	/**
    * @var Txn $txn
    */
    public function map($txn): array
    {
        return [
            $txn->user_id,
            $txn->id,
            $txn->type_label,
            $txn->stat_label,
            $txn->src_des,
            $txn->ref_no,
            $txn->bank_ref_no,
            $txn->opening_balance,
            $txn->amount,
            $txn->fee_amount,
            $txn->balance,
            ($txn->created_at == '0000-00-00 00:00:00' || $txn->created_at == '-0001-11-30 00:00:00') ? '' :
            date('d-m-Y | H:i:s',strtotime($txn->created_at)),
        ];
    }

    public function query()
    {
        $query = Txn::select('id','user_id','created_at','type','stat','src_des','ref_no','bank_ref_no','opening_balance','amount','fee_amount','balance','created_at')
                ->orderBy('id','desc');

        //Get user id
        if (isset($this->request['vg_id']) && ! empty($this->request['vg_id'])) {
            $user = WalletUser::where('vg_id',$this->request['vg_id'])->first();
            $this->request['user_id'] = $user->id ?? null;
        }
        if (isset($this->request['email']) && ! empty($this->request['email'])) {
            $user = WalletUser::where('email',$this->request['email'])->first();
            $this->request['user_id'] = $user->id ?? null;
        }
        if (isset($this->request['phone']) && ! empty($this->request['phone'])) {
            $user = WalletUser::where('phone',$this->request['phone'])->first();
            $this->request['user_id'] = $user->id ?? null;
        }

        if (isset($this->request['id']) && ! empty($this->request['id']))
            $query->where('id',$this->request['id']);
        if (isset($this->request['stat']) && ! empty($this->request['stat']))
            $query->where('stat',$this->request['stat']);
        if ((isset($this->request['user_id']) && ! empty($this->request['user_id'])))
            $query->where('user_id',$this->request['user_id']);
        if ((isset($this->request['ref_no']) && ! empty($this->request['ref_no'])))
            $query->where('ref_no',$this->request['ref_no']);
        if ((isset($this->request['src_des']) && ! empty($this->request['src_des'])))
            $query->where('src_des',$this->request['src_des']);
        if ((isset($this->request['encrypted_id']) && ! empty($this->request['encrypted_id'])))
            $query->where('encrypted_id',$this->request['encrypted_id']);
        if ((isset($this->request['type']) && ! empty($this->request['type'])))
            $query->where('type',$this->request['type']);        
        if ((isset($this->request['from_date']) && ! empty($this->request['from_date'])))
            $query->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime($this->request['from_date'])));
        if ((isset($this->request['to_date']) && ! empty($this->request['to_date'])))
            $query->where('created_at', '<=', date('Y-m-d 25:59:59', strtotime($this->request['to_date'])));

        return $query;
    }
}

Mình có thử dd() trong file Xlsx.php thì thấy là nếu mình bỏ Interface ShouldQueue thì nó sẽ không gọi vào Xlsx::readColumnsAndRowsAttributes() nữa.

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí