Search code examples
htmllaravelexport-to-excelmaatwebsite-excel

Problem using maatwebsite/excel: C:\Users\<My user>\AppData\Local\Temp\laravel-excel-k5R8qyVIzkH8X0m5YwHyC2IOznrThvdk is an Invalid HTML file


I'm trying to make an excel export via maatwebsite/excel, but I keep getting the following error:

C:\Users\<My Laravel project directory>\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Html.php:592

public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)

{

    // Validate

    if (!$this->canRead($pFilename)) {

        throw new Exception($pFilename . ' is an Invalid HTML file.');

    }

My Controller:

return Excel::download(new ContractsExport($contracts, $active), 'solic_contrat_' . date('d-m-Y') . '_' . time() . '.xlsx');

My export file:

<?php

namespace App\Exports;

use App\Contract;
#use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ContractsExport implements FromView
{
    use Exportable;

    protected $contracts;
    protected $active;

    public function __construct($contracts = null, $active = null)
    {
        $this->contracts = $contracts;
        $this->active = $active;
    }

    public function view(): View
    {
        $contracts = $this->contracts;
        $active = $this->active;
        return view("exports.contracts", compact("contracts", "active"));
    }
}

The blade template:

<table>
    <thead>
        <tr>
            <th><b>N°</b></th>
            <th><b>RUT</b></th>
            <th><b>Apellido Paterno</b></th>
            <th><b>Apellido Materno</b></th>
            <th><b>Nombre</b></th>
            <th><b>Cargo</b></th>
            <th><b>Fecha de inicio</b></th>
            <th><b>Fecha de término</b></th>
            <th><b>Causal de contratación</b></th>
            <th><b>Tope del causal</b></th>
            <th><b>Lugar de trabajo</b></th>
            <th><b>Estado</b></th>
        </tr>
    </thead>
    <tbody>
        @php
        $k = 0;
        @endphp
        @foreach ($contracts as $contract)
        @php
        $k++;
        @endphp
        <tr>
            <td>{{ $k }}</td>
            <td>{{ $contract->rut }}</td>
            <td>{{ $contract->f_lname }}</td>
            <td>{{ $contract->m_lname }}</td>
            <td>{{ $contract->name }}</td>
            <td>{{ $contract->search->position->name }}</td>
            @php
            $startdate = new \Carbon\Carbon($contract->search->start_date);
            @endphp
            <td>{{ $startdate->format('d/m/Y') }}</td>
            @php
            $enddate = new \Carbon\Carbon($contract->search->date);
            @endphp
            <td>{{ $enddate->format('d/m/Y') }}</td>
            <td>{{ $contract->search->causal_service }}</td>
            @switch($contract->search->causal_service)
            @case('Reemplazo por motivo de licencia médica')
            @case('Reemplazo por motivo de vacaciones')
            <td>Sin tope de días</td>
            @break
            @case('Proyecto nuevos y específicos')
            @php
            $date = $contract->search->start_date;
            echo '<td>' . date('d/m/Y', strtotime($date. '+ 180 days')) . '</td>';
            @endphp
            @break
            @case('Trabajos urgentes')
            @case('Evento extraordinario')
            @case('Aumento ocasional')
            @php
            $date = $contract->search->start_date;
            echo '<td>' . date('d/m/Y', strtotime($date. '+ 90 days')) . '</td>';
            @endphp
            @break
            @endswitch

            <td>{{ $contract->search->address }}</td>

            @switch($contract->status)
            @case('pending')
            <td>Por Aprobar</td>
            @break
            @case('approved')
            <td>Aprobada</td>
            @break
            @case('rejected')
            <td>Rechazada</td>
            @break
            @case('cancelled')
            <td>Cancelada</td>
            @break
            @case('process')
            <td>En Proceso</td>
            @break
            @default
            <td>Estado no permitido</td>
            @endswitch

        </tr>
        @endforeach
    </tbody>

</table>

And the result (laravel-excel-k5R8qyVIzkH8X0m5YwHyC2IOznrThvdk):

<table>
    Solicitudes de Contrataciones
    <thead>
        <tr>
            <th><b>N°</b></th>
            <th><b>RUT</b></th>
            <th><b>Apellido Paterno</b></th>
            <th><b>Apellido Materno</b></th>
            <th><b>Nombre</b></th>
            <th><b>Cargo</b></th>
            <th><b>Fecha de inicio</b></th>
            <th><b>Fecha de término</b></th>
            <th><b>Causal de contratación</b></th>
            <th><b>Tope del causal</b></th>
            <th><b>Lugar de trabajo</b></th>
            <th><b>Estado</b></th>
        </tr>
    </thead>
    <tbody>
                                <tr>
            <td>1</td>
            <td>20.284.123</td>
            <td>Alonso</td>
            <td>Gonzalez</td>
            <td>Daniela</td>
            <td>Ejecutivo de Ventas</td>
                        <td>23/11/2019</td>
                        <td>25/12/2019</td>
            <td>Trabajos urgentes</td>
                                                <td>21/02/2020</td>            
            <td>Esperanza 77, Santiago</td>

                        <td>En Proceso</td>

        </tr>
                        <tr>
            <td>2</td>
            <td>22.143.965</td>
            <td>Moreno</td>
            <td>Vega</td>
            <td>Jesús</td>
            <td>Secretaria</td>
                        <td>20/11/2019</td>
                        <td>25/12/2019</td>
            <td>Evento extraordinario</td>
                                    <td>18/02/2020</td>            
            <td>Apoquindo 4000, Las Condes</td>

                        <td>Por Aprobar</td>

        </tr>
                        <tr>
            <td>3</td>
            <td>25.334.235</td>
            <td>Pérez</td>
            <td>Rodriguez</td>
            <td>Juan</td>
            <td>Secretaria</td>
                        <td>20/11/2019</td>
                        <td>25/12/2019</td>
            <td>Evento extraordinario</td>
                                    <td>18/02/2020</td>            
            <td>Apoquindo 4000, Las Condes</td>

                        <td>Por Aprobar</td>

        </tr>
            </tbody>

</table>

There must be something I'm missing from the Laravel-excel documentation, but right now I'm just walking in circles.

Thanks in advance to anyone who wishes to help me


Solution

  • I found the solution for the problem: My .blade.php file was saved with codification UTF-8 with BOM. Saving the view with codification UTF-8 solved the issue.