Laravel Excel 中的一些小细节
excel导出后,显示科学计数
以往的方式都是在后面加"\t",加一些单引号的字符 这种方式能解决问题,但对数据进行来污染
通过以下方式进行解决
ps:还是要多看国外文档呀
代码如下
<?php
namespace App\Admin\Extensions;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
class AdmissionsExporter extends AbstractExporter implements FromQuery, WithHeadings,WithCustomValueBinder
{
use Exportable;
public function bindValue(Cell $cell, $value)
{
$cell->setValueExplicit($value, DataType::TYPE_STRING);
return true;
}
/**
* @var string
*/
protected $fileName='考生状态表.xlsx';
/**
* @var array
*/
protected $headings = [];
/**
* @var array
*/
protected $columns = [
'name' => '姓名',
'idcard'=>'身份证号',
'exam_id'=>'准考证号',
'exam_type'=>'考生类别',
'payment_status'=>'缴费状态',
'from'=>'缴费来源'
];
/**
* @return array
*/
public function headings(): array
{
if (!empty($this->columns)) {
return array_values($this->columns);
}
return $this->headings;
}
/**
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
*/
public function query()
{
if (!empty($this->columns)) {
$columns = array_keys($this->columns);
$eagerLoads = array_keys($this->getQuery()->getEagerLoads());
$columns = collect($columns)->reject(function ($column) use ($eagerLoads) {
return Str::contains($column, '.') || in_array($column, $eagerLoads);
});
return $this->getQuery()->select($columns->toArray());
}
return $this->getQuery();
}
/**
* {@inheritdoc}
*/
public function export()
{
$this->download($this->fileName)->prepare(request())->send();
exit;
}
}