multi-store/app/common/service/xlsx/ActivityZoneService.php

123 lines
4.3 KiB
PHP

<?php
namespace app\common\service\xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class ActivityZoneService
{
public function export($data, $title, $remark)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->mergeCells('A1:F1');
$sheet->mergeCells('A2:B2');
$sheet->mergeCells('C2:D2');
$sheet->mergeCells('E2:F2');
$sheet->mergeCells('A3:B3');
$sheet->mergeCells('C3:D3');
$sheet->mergeCells('E3:F3');
$sheet->mergeCells('A4:B4');
$sheet->mergeCells('C4:D4');
$sheet->mergeCells('E4:F4');
$sheet->mergeCells('A5:F5');
$sheet->mergeCells('B6:C6');
$sheet->setCellValue('A1', $title);
$sheet->setCellValue('A2', '姓名:');
$sheet->setCellValue('C2', '电话:');
$sheet->setCellValue('E2', '会员角色:');
$sheet->setCellValue('A3', '下单时间:');
$sheet->setCellValue('C3', '到货时间:');
$sheet->setCellValue('E3', '订单编号:');
$sheet->setCellValue('A4', '办事时间:');
$sheet->setCellValue('C4', '寿宴当日桌数:');
$sheet->setCellValue('E4', '厨师:');
$sheet->setCellValue('A5', '收货地址:');
$sheet->setCellValue('A6', '编号');
$sheet->setCellValue('B6', '品名');
$sheet->setCellValue('D6', '单位');
$sheet->setCellValue('E6', '下单数量');
$sheet->setCellValue('F6', '客户备注');
// 设置默认的单元格样式
$defaultStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
];
// 应用默认样式到整个工作表
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
// 设置默认的单元格样式
$leftStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_LEFT,
'vertical' => Alignment::VERTICAL_CENTER,
],
];
// 应用默认样式到A2:F5
$sheet->getStyle('A2:F5')->applyFromArray($leftStyle);
$column = 7;
foreach ($data as $k => $item) {
$sheet->mergeCells("A{$column}:F{$column}");
$sheet->setCellValue('A' . $column, $k);
$sheet->getStyle('A' . $column)->applyFromArray([
'font' => [
'bold' => true,
'size' => 16,
],
]);
$column++;
foreach ($item as $value) {
$sheet->mergeCells("B{$column}:C{$column}");
$sheet->setCellValue('A' . $column, $value['id']);
$sheet->setCellValue("B{$column}", $value['store_name']);
$sheet->setCellValue("D{$column}", $value['unit_name']);
$column++;
}
}
$sheet->mergeCells('A' . $column . ':F' . $column);
$sheet->setCellValue('A' . $column, "备注:{$remark}");
$sheet->getRowDimension($column)->setRowHeight(50);
$sheet->getStyle('A' . $column)->getAlignment()->setWrapText(true);
$sheet->getStyle('A' . $column. ':' . 'F' . $column)->applyFromArray($leftStyle);
// 设置单元格的样式
$styleArray = [
'font' => [
'bold' => true,
'size' => 28,
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
// 定义线框样式
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN, // 线框样式
'color' => ['argb' => '000000'], // 线框颜色
],
],
];
$sheet->getStyle('A1:F' . $column)->applyFromArray($styleArray);
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . $title . date('YmdHi') . '.xlsx';
$file_path = public_path() . $url;
if (!is_dir(dirname($file_path))) {
mkdir(dirname($file_path), 0777, true);
}
$writer->save($file_path);
return getenv('APP_URL') . $url;
}
}