68 lines
1.9 KiB
PHP
68 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadings; //设置标题
|
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison; //为空时零填充
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize; //自动单元格尺寸
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; //设置单元格数据格式
|
|
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
class NewUserExport implements FromCollection ,WithHeadings,WithStrictNullComparison,WithColumnFormatting,ShouldAutoSize
|
|
{
|
|
private $data;
|
|
public function __construct(array $data)
|
|
{
|
|
$this->data = $data;
|
|
}
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'id',
|
|
'姓名',
|
|
'昵称',
|
|
'手机',
|
|
'性别',
|
|
'年龄',
|
|
'学历',
|
|
'行业',
|
|
'信仰',
|
|
'常居地',
|
|
'情感状态',
|
|
'收入',
|
|
];
|
|
}
|
|
//设置列格式
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'A' => NumberFormat::FORMAT_TEXT,
|
|
'D' => NumberFormat::FORMAT_TEXT,
|
|
];
|
|
}
|
|
public function collection()
|
|
{
|
|
$data = [];
|
|
foreach($this->data as $row) {
|
|
switch($row["sex"]) {
|
|
case 1:
|
|
$sex = "男";
|
|
break;
|
|
case 2:
|
|
$sex = "女";
|
|
break;
|
|
default:
|
|
$sex = "未知";
|
|
}
|
|
$data[] = [
|
|
$row["user_id"], $row["name"], $row["nickname"], $row["mobile"], $sex, $row["age"],
|
|
$row["degree"], $row["industry"]."/".$row["industry_sub"], $row["belief"],
|
|
$row["province"]."-".$row["city"], $row['state'], $row["income"]
|
|
];
|
|
}
|
|
$data = collect($data);
|
|
return $data;
|
|
}
|
|
|
|
}
|