laravel-admin实战做个简单的客户管理——数据表格Laravel-Excel导出简单示例

    |     2020年12月31日   |   学习偶记   |     评论已关闭   |    2884

1、先安装Laravel-Excel

2、建立一个导出类,FrontCustomerExporter。

3、在FrontCustomerController中use它。

4、在grid方法中使用

$grid->exporter(new ExcelFile(new FrontCustomer())); //使用excel类,将model传递过去,方便自定义格式。

5、文件结构: 自定义输出excel类 6、最终效果: excel导出   FrontCustomerExporter.php完整代码: namespace App\Admin\Exceptions;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison; //将0直接显示,不显示为空格
use Maatwebsite\Excel\Concerns\ShouldAutoSize; //表格自动宽度
use Maatwebsite\Excel\Concerns\WithColumnWidths; //可以和表格自动宽度一起使用,仅仅指定的列宽度不能自动调整
use Maatwebsite\Excel\Concerns\WithStyles; //设置表格样式
use Maatwebsite\Excel\Concerns\WithProperties; //定义文本属性,比如作者,日期等等
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;//设置表格样式
use PhpOffice\PhpSpreadsheet\Style\Color as Color; //颜色
use PhpOffice\PhpSpreadsheet\Style\Alignment as Alignment; //对齐
use PhpOffice\PhpSpreadsheet\Style\Fill as Fill; //填充
use PhpOffice\PhpSpreadsheet\Style\Border as Border; //边框
use PhpOffice\PhpSpreadsheet\RichText\RichText as RichText; //富文本
use PhpOffice\PhpSpreadsheet\Cell\DataType as DataType; //单元格数据类型

class FrontCustomerExporter extends ExcelExporter implements WithMapping, WithStrictNullComparison, ShouldAutoSize, WithColumnWidths, WithStyles, WithProperties
{
//导出的文件名,中文不行
protected $fileName = 'FrontCustomer.xlsx';
//需要导出的列名
protected $columns = [
'id' => 'ID',
'user_name' => '用户名',
'user_type' => '用户类型',
'user_ip' => '最后一次IP',
'login_cnt' => '登录次数',
'locked' => '冻结状态',
'amount' => '账户金额',
'consumed' => '已消费',
'created_at' => '创建时间',
'updated_at' => '更新时间'
];

protected $model = null;

//如果不需要model 可以删除这个方法
public function __construct($model = null)
{
//有了数据,就可以做各种函数,合并单元格等等操作
$this->model = $model;
}

//文档属性摘要
public function properties(): array
{
return [
'creator' => 'MaQi', //作者
'lastModifiedBy' => 'MaQi', //最后修改者
'title' => '客户列表',
'description' => date('Y-m-d H:i:s',time()).'后台导出的客户列表',
'subject' => '客户列表',
//'keywords' => '客户,表格', //关键词
//'category' => '客户', //分类
//'manager' => 'Patrick Brouwers',
//'company' => 'NaQi',
];
}

//设定表格宽度
public function columnWidths(): array
{
return [
'A' => 5,
'C' => 10,
'D' => 25,
'E' => 10,
'F' => 10,
'G' => 10,
'H' => 10
];
}

//根据设置表格样式
public function styles(Worksheet $sheet)
{
//冻结字段已经冻结的值需要设置红色
$red_field = 'locked';
//设置用户名为文本类型,因为有手机号,会被当做数字处理。
$str_field = 'user_name';
//设置默认行高,但是有数据的不行。得用getRowDimension指定行来处理
$sheet->getDefaultRowDimension()->setRowHeight(20);
$model_array = $this->model->get()->toArray();
//数据的条数 注意表头
$count = count($model_array);
for($i = 0; $i < $count; $i++)
{
//要去掉表头,并且$i是从0开始,所以需要加2
$j = $i + 2;
$sheet->getRowDimension($j)->setRowHeight(20);
//如果冻结,则字体设置为红色
if($model_array[$i][$red_field])
{
$sheet->getStyle('F'.$j)->getFont()->getColor()->setARGB(Color::COLOR_RED); //设置为红色 粗体
$sheet->getStyle('F'.$j)->getFont()->setBold(true);
}
//设置用户名为文本类型,因为有手机号,会被当做数字处理。
$sheet->getCell('B'.$j)
->setValueExplicit(
$model_array[$i][$str_field],
DataType::TYPE_STRING2
);
//如果是手机用户,背景色为粉红色
if($model_array[$i]['user_type'] == 'mobile')
{
$sheet->getStyle('B'.$j)->getFill()->applyFromArray(
[
'fillType' => Fill::FILL_SOLID, //没有格式
'rotation' => 0, //旋转
'startColor' => [
'rgb' => 'FFC0CB'
],
'endColor' => [
'argb' => 'FFC0CB'
]
]
);
}
}

//将标题的字体加粗
$styleArray = [
'font' => [
'name' => '微软雅黑',
'bold' => true,
'italic' => false,
'underline' => false, //Font::UNDERLINE_DOUBLE, //下划线
'strikethrough' => false, //删除线
'color' => [
'rgb' => '808080'
]
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER, //水平居中
'vertical' => Alignment::VERTICAL_CENTER, //垂直居中
'wrapText' => true,
]
];
$sheet->getStyle('A1:J1')->applyFromArray($styleArray);
$sheet->getRowDimension(1)->setRowHeight(25); //第一行标题高度
//合并最底下单元格,进行富文本操作。
$bottom = $count + 2;
$sheet->mergeCells('A'.$bottom.':J'.$bottom);
//底部添加富文本
$richText = new RichText();
$richText->createText('note:');
$payable = $richText->createTextRun('可以联系管理员进行复杂的');
$payable->getFont()->setName('宋体')->setSize(9);
//设置文字倾斜,黑体,并且有颜色,注意wps无法显示这个效果
$payable = $richText->createTextRun(' 函数公式计算 ');
$payable->getFont()->setBold(true)->setName('宋体')->setSize(9);
$payable->getFont()->setColor(new Color (Color::COLOR_RED));
$payable = $richText->createTextRun('表格导出.');
$payable->getFont()->setName('宋体')->setSize(9);
$sheet->getCell('A'.$bottom)->setValue($richText);
}

//根据数据进行映射
public function map($user) : array
{
switch ($user->user_type)
{
case 'mobile':
$user_type = '手机';
break;
case 'wechat':
$user_type = '微信';
break;
case 'email':
$user_type = '邮箱';
break;
default:
$user_type = '错误类型';
}
return [
$user->id,
$user->user_name,
$user_type,
$user->user_ip,
$user->login_cnt,
$user->locked ? '冻结' : '正常', // 字段数据替换
$user->amount,
$user->consumed,
$user->created_at,
$user->updated_at
];
}
}

噢!评论已关闭。