Alauddin Ahmed
Alauddin Ahmed

Reputation: 1185

Export to XML from CSV Laravel-Excel

I'm using Laravel-Excel (version-3) 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 [];
    }
}

My intention is to read csv files and convert it to XML. So if anyone have better option for that, you can suggest it too.

Upvotes: 2

Views: 2045

Answers (0)

Related Questions