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

116 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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, $typeName, $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', "供 投 里 海 农 特 产 品 下 单 清 单({$typeName}");
$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);
foreach ($data as $k => $v) {
$column = $k + 7;
$sheet->mergeCells("B{$column}:C{$column}");
$sheet->setCellValue('A' . ($k + 7), $v['id']);
$sheet->setCellValue("B{$column}", $v['store_name']);
$sheet->setCellValue("D{$column}", $v['unit_name']);
}
$count = count($data);
$sheet->mergeCells('A' . ($count + 7) . ':F' . ($count + 7));
$sheet->setCellValue('A' . ($count + 7), "专区类型:{$typeName}");
$sheet->mergeCells('A' . ($count + 8) . ':F' . ($count + 8));
$sheet->setCellValue('A' . ($count + 8), "");
$sheet->mergeCells('A' . ($count + 9) . ':F' . ($count + 9));
$sheet->setCellValue('A' . ($count + 9), "备注:{$remark}");
$sheet->getRowDimension($count + 9)->setRowHeight(50);
$sheet->getStyle('A' . ($count + 9))->getAlignment()->setWrapText(true);
$sheet->getStyle('A' . ($count + 7). ':' . 'F' . ($count + 9))->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' . ($count + 9))->applyFromArray($styleArray);
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . "/供投里海农特产品下单清单({$typeName}" . 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;
}
}