使用 maatwebsite/excel 时出现问题:C:\Users\<My user>\AppData\Local\Temp\laravel-excel-k5R8qyVIzkH8X0m5YwHyC2IOznrThvdk 是一个无效的 HTML 文件
Problem using maatwebsite/excel: C:\Users\<My user>\AppData\Local\Temp\laravel-excel-k5R8qyVIzkH8X0m5YwHyC2IOznrThvdk is an Invalid HTML file
我正在尝试通过 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"));
}
}
blade 模板:
<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 文档中一定有我遗漏的东西,但现在我只是在原地打转。
提前感谢任何愿意帮助我的人
我找到了问题的解决方案:
我的 .blade.php
文件保存为带有 BOM 的编码 UTF-8。
使用编码 UTF-8 保存视图解决了这个问题。
我正在尝试通过 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"));
}
}
blade 模板:
<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 文档中一定有我遗漏的东西,但现在我只是在原地打转。
提前感谢任何愿意帮助我的人
我找到了问题的解决方案:
我的 .blade.php
文件保存为带有 BOM 的编码 UTF-8。
使用编码 UTF-8 保存视图解决了这个问题。