官方文档
Laravel-Excel一共有三个版本,2.1====>3.0====>3.1。我们先来看下每个版本的跟Laravel框架以及PHP的兼容情况,见下图。
LZ用过了三个版本。。。如果你是刚刚开始使用这个扩展包,只要环境支持就一定要用3.1版本。先不说3.1的设计更加优雅。首先3.0没有导入功能,emm没错,当时好不容易等到新的版本发布居然没有导入。。and 3.8不向下兼容2.*。so好像也没什么理由不选3.1了hhh
class OrderExport extends ExcelExporter implements WithMapping, WithStrictNullComparison
{
protected $fileName = '订单列表.xlsx';
protected $columns = [
'product_id' => '产品名称',
'position_id' => '产品链接',
'company_id' => '公司名称',
'recipient' => '收件人',
'mobile' => '联系电话',
'province' => '详细地址',
'city' => '详细地址',
'county' => '详细地址',
'house_number' => '详细地址',
'order_begin_time' => '下单时间',
'price' => '产品单价',
'count' => '数量',
'express_fee' => '快递费',
// 'goods_type' => '物品类型',
'order_close_time' => '订单完成时间',
'note' => '备注',
'express_no' => '快递单号',
'express_company' => '快递公司',
'order_status' => '订单状态',
'pay_method' => '付款方式',
'safeguard_status' => '维权状态',
'address_valid' => '地址有效性',
'created_at' => '创建时间',
];
public function map($row): array
{
return [
data_get($row, 'product.name'),
data_get($row, 'position.link'),
data_get($row, 'company.name'),
$row->recipient,
$row->mobile,
$row->province . $row->city . $row->county . $row->house_number,
$row->order_begin_time,
$row->price,
$row->count,
$row->express_fee,
// $row->goods_type,
$row->order_close_time,
$row->note,
$row->express_no,
$row->express_company,
Tools::orderStatusMap()[$row->order_status ?: '0'],
Tools::payMethodMap()[$row->pay_method ?: '0'],
Tools::safeguardtSatusMap()[$row->safeguard_status ?: '0'],
Tools::addressValidMap()[$row->address_valid ?: '0'],
$row->created_at,
];
}
}
这样会导致导出的表头出现四个重复的详细地址字段
有没有很好奇这里的表头是怎么设置出来。。然后看了一下生成的导出类会继承ExcelExporter类
abstract class ExcelExporter extends AbstractExporter implements FromQuery, WithHeadings
{
use Exportable;
/**
* @var string
*/
protected $fileName;
/**
* @var array
*/
protected $headings = [];
/**
* @var array
*/
protected $columns = [];
/**
* @return array
*/
public function headings(): array
{
if (!empty($this->columns)) {
return array_values($this->columns);
}
return $this->headings;
}
ExcelExporter类里面的headings方法直接从的导出类里获取到columns变量,so外面可以在自己生成的导出类重写一下headings方法
class OrderExport extends ExcelExporter implements WithMapping, WithStrictNullComparison, WithHeadings
{
protected $fileName = '订单列表.xlsx';
// 添加headings方法,去重columns的值
public function headings(): array
{
if (!empty($this->columns)) {
return array_values(array_unique($this->columns));
}
return parent::headings();
}
// 设置需要从库中检索出的字段
protected $columns = [
'product_id' => '产品名称',
'position_id' => '产品链接',
'company_id' => '公司名称',
'recipient' => '收件人',
'mobile' => '联系电话',
'province' => '详细地址',
'city' => '详细地址',
'county' => '详细地址',
'house_number' => '详细地址',
'order_begin_time' => '下单时间',
'price' => '产品单价',
'count' => '数量',
'express_fee' => '快递费',
'order_close_time' => '订单完成时间',
'note' => '备注',
'express_no' => '快递单号',
'express_company' => '快递公司',
'order_status' => '订单状态',
'pay_method' => '付款方式',
'safeguard_status' => '维权状态',
'address_valid' => '地址有效性',
'created_at' => '创建时间',
];
// 设置映射字段
public function map($row): array
{
return [
data_get($row, 'product.name'),
data_get($row, 'position.link'),
data_get($row, 'company.name'),
$row->recipient,
$row->mobile,
$row->province . $row->city . $row->county . $row->house_number,
$row->order_begin_time,
$row->price,
$row->count,
$row->express_fee,
$row->order_close_time,
$row->note,
$row->express_no,
$row->express_company,
Tools::orderStatusMap()[$row->order_status ?: '0'],
Tools::payMethodMap()[$row->pay_method ?: '0'],
Tools::safeguardtSatusMap()[$row->safeguard_status ?: '0'],
Tools::addressValidMap()[$row->address_valid ?: '0'],
$row->created_at,
];
}
}
进行如下修改后就可以正常导出了。。。
今天先写到这里了。。下班啦啦啦