使用maatwebsite / excel的问题:C:\ Users \ \ AppData \ Local \ Temp \ laravel-excel-k5R8qyVIzkH8X0m5YwHyC2IOznrThvdk是无效的HTML文件

问题描述 投票:0回答:1

我正在尝试通过maatwebsite / excel进行excel导出,但始终出现以下错误:

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.');

    }

我的控制器:

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

我的导出文件:

<?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"));
    }
}

刀片模板:

<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>

[[结果(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>
[Laravel-excel文档中肯定缺少某些内容,但是现在我只是在圈子里走路。

提前感谢任何希望帮助我的人

html laravel export-to-excel maatwebsite-excel
1个回答
0
投票
我找到了解决问题的方法:我的.blade.php文件保存为带有BOM的UTF-8编码。使用编码UTF-8保存视图可以解决此问题。
© www.soinside.com 2019 - 2024. All rights reserved.