PHP必备技能 -- Excel 导出之Maatwebsite\Excel 合并单元格和设置字体
在日常的项目中,经常会用导出 Excel 表格的需求,我们在使用 laravel 时怎么进行处理呢,尤其是涉及到合并单元格时,就会很麻烦。下面介绍一种使用Maatwebsite\Excel合并单元格的方式。我们要合并的单元格如截图所示:
详细的实现代码如下:
最后不要忘记 执行导出呦。
return Excel::download(
new OrderExport($res->obj),
“xxxxxx.xlsx”,
);
<?php
namespace App\Modules\Order\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\DefaultValueBinder;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithEvents;
class OrderExport extends DefaultValueBinder implements FromCollection, WithHeadings, WithTitle, ShouldAutoSize, WithEvents
{
/**
* @var int|mixed
*/
private mixed $totalSkuQuantity;
/**
* @var array|mixed
*/
private mixed $exportData;
/**
* @var mixed|string
*/
private mixed $exportDate;
public function __construct(protected array $data = [])
{
$this->exportData = $data['data'] ?? [];
$this->totalSkuQuantity = $data['totalSkuQuantity'] ?? 0;
$this->exportDate = $data['exportDate'] ?? date('Y-m-d H:i:s');
}
public static function afterSheet(AfterSheet $event): void
{
$sheet = $event->sheet->getDelegate();
$sheet->mergeCells('A1:N1');
$sheet->mergeCells('A2:H2');
$sheet->mergeCells('A3:H3');
$styleArray = [
'font' => [
'size' => 16
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
];
$sheet->getStyle('A1:N1')->applyFromArray($styleArray);
}
public function registerEvents(): array
{
return [
AfterSheet::class => [self::class, 'afterSheet'],
];
}
public function title(): string{
return '调拨单-导出汇总';
}
public function headings(): array
{
return [
[
'调拨单-导出汇总'
],
['表单生成日期:'.$this->exportDate],
['商品种类总数:'.$this->totalSkuQuantity.'个'],
[
'', '', '', '', '', '', '', '', '', '', '', '', '', ''
],
[
'序号', '调出门店', '调入门店', '出库单号', '调拨状态', '商品名称', 'UPC', '类目', '规格', '采购单位', '采购单价', '调拨数量', '剩余库存', '操作人'
]
];
}
/**
* @throws Exception
*/
public function bindValue(Cell $cell, $value): bool
{
$column = $cell->getColumn();
if (in_array($column, ['G', 'I'])) {
$cell->setValueExplicit($value, DataType::TYPE_STRING);
return true;
}
return parent::bindValue($cell, $value);
}
public function collection(): Collection
{
return new Collection($this->getData());
}
public function getData(): array
{
$rows = [];
if($this->exportData){
foreach ($this->exportData as $item){
$rows[] = [
$item['transfer_order_no'] ?? '',//序号
$item['store_name'] ?? '',//调出门店
$item['to_store_name'] ?? '',//调入门店
$item['outbound_no'] ?? '',//出库单号
$item['status_text'] ?? '',//调拨状态
$item['product_name'] ?? '',//商品名称
$item['upc'] ?? '',//UPC
$item['sg_tag_name'] ?? '',//类目
$item['sku_name'] ?? '',//规格
$item['stock_unit'] ?? '',//采购单位
$item['origin_purchase_price'] ?? '',//采购单价
$item['total_plan_quantity'] ?? '',//调拨数量
$item['usable_stock'] ?? '',//剩余库存
$item['create_username'] ?? '',//操作人
];
}
}
return $rows;
}
}
```> PHP必备技能 -- Excel 导出之Maatwebsite\Excel 合并单元格和设置字体!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: