SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.29k stars 1.92k forks source link

Export to XML showing error #2479

Closed rassemdev closed 4 years ago

rassemdev commented 4 years ago

I'm using Laravel-Excel v3.1 with(PHP v7.2.24, Laravel v6.2) to import/export data into various formats. But i'm having trouble while exporting to XML. When i'm exporting to xml the following error occurring No writer found for type Xml. I'm not getting any clue or resources to solve this problem. Exporting to other format (csv, xlsx) working fine. Can anyone help me out with this one! Thank You.

Here is my code: In Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\LoanInfoExport;

class HomeController extends Controller
{
    public function storeLoanInfo(Request $request)
    {
        $path = $request->file('upload_loan_info')->getRealPath();
        $data = \Excel::toArray('', $path, null, \Maatwebsite\Excel\Excel::TSV)[0];

        $loanInfoForExport = [];
        foreach ($data as $key => $value) {
            $disbursedValue = str_replace(',', '', $value[6]);
            $outstandingValue = str_replace(',', '', $value[7]);

            if (!empty($value[1])) {
                array_push($loanInfoForExport, [
                    'disbursed_date' => $value[0],
                    'borrower' => $value[1],
                    'loan_product' => $value[2],
                    'loan_id' => $value[3],
                    'interest' => $value[4],
                    'duration' => $value[5],
                    'disbursed' => $disbursedValue,
                    'outstanding' => $outstandingValue,
                    'status' => $value[8],
                ]);
            }
        }

        return (new LoanInfoExport($loanInfoArray))->download('invoices.xml', \Maatwebsite\Excel\Excel::XML);

    }
}

Then LoanInfoExport.php:

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

class LoanInfoExport implements FromCollection, WithHeadings, WithStrictNullComparison
{
    use Exportable;

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

    public function collection()
    {
        return collect($this->data);
    }

    public function headings(): array
    {
        return [];
    }
}

I'm using Ubuntu 18.04 with Lamp server.

ghost commented 4 years ago

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/3.1/getting-started/contributing.html) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

patrickbrouwers commented 4 years ago

PhpSpreadsheet doesn't have an XML writer. The Excel::XML type is only available as reader.

ellie-ochieno commented 1 year ago

I'm running ubuntu 22.04 on my hosting server and I managed to get around this eror

sudo apt-get install php-xml sudo snap install xmlstarlet

and then restarting the server for

sudo systemctrl restart nginx.service