<?php
namespace App\Api\Exports;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ExportMerge implements WithStyles
{
protected $data = [];
protected $z_count;
protected $s_count;
public function __construct($data, $z_count, $s_count)
{
$this->data = $data;
$this->s_count = $s_count;
$this->z_count = $z_count;
}
public function styles(Worksheet $sheet)
{
$code = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
//设置表头
$sheet->mergeCells('A1:A2'); //合并
$sheet->mergeCells('B1:B2');
$sheet->getCell('A1')->setValue($this->data[0][0]); //在合并的单元格设置值
$sheet->getCell('B1')->setValue($this->data[0][1]);
$sheet->mergeCells('C1:' . ($code[$this->s_count + 1]) . '1'); //第一个
$sheet->mergeCells($code[$this->s_count + 2] . '1:' . $code[$this->s_count + $this->z_count + 1] . '1');//第二个
$sheet->getCell('C1')->setValue("收入");
$sheet->getCell($code[$this->s_count + 2] . '1')->setValue("支出");
//后面3个
$sheet->mergeCells($code[$this->s_count + $this->z_count + 2] . '1:' . $code[$this->s_count + $this->z_count + 2] . '2');
$sheet->mergeCells($code[$this->s_count + $this->z_count + 3] . '1:' . $code[$this->s_count + $this->z_count + 3] . '2');
$sheet->mergeCells($code[$this->s_count + $this->z_count + 4] . '1:' . $code[$this->s_count + $this->z_count + 4] . '2');
????????//设置数据体
$jj = 3;
foreach ($this->data as $key => $vo) {
if ($key == 0) {
$array_slice = array_slice($code, 0, $this->s_count + $this->z_count + 5);
$ii = 0;
foreach ($array_slice as $k => $v) {
if (in_array($v, array_slice($code, count($array_slice) - 3, 3))) {
$sheet->getCell($v . '1')->setValue($vo[$ii]);
} else {
$sheet->getCell($v . '2')->setValue($vo[$ii]);
}
$ii++;
}
} else {
$array_slice = array_slice($code, 0, $this->s_count + $this->z_count + 5);
$jj1 = 0;
foreach ($vo as $kk => $vv) {
$sheet->getCell($array_slice[$jj1] . $jj)->setValue($vv);
$jj1++;
}
$jj++;
}
}
}
}
//设置excel的值,其实就是在合并单元格后,找到合并后的单元格的新坐标设置值
$sheet->getCell('C1')->setValue("收入");
|