lihaiMiddleOffice/app/common/service/xlsx/StoreProductPriceXlsx.php
2025-03-12 11:19:42 +08:00

79 lines
2.9 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 StoreProductPriceXlsx
{
public function export($data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', '商品ID');
$sheet->setCellValue('B1', '商品');
$sheet->setCellValue('C1', '采购价');
$sheet->setCellValue('D1', '供货价比例');
$sheet->setCellValue('E1', '供货价');
$sheet->setCellValue('F1', '商户价比例');
$sheet->setCellValue('G1', '商户价');
$sheet->setCellValue('H1', '会员价比例');
$sheet->setCellValue('I1', '会员价');
$sheet->setCellValue('J1', '零售价比例');
$sheet->setCellValue('K1', '零售价');
$sheet->setCellValue('L1', '时间');
// 设置默认的单元格样式
$defaultStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
];
// 应用默认样式到整个工作表
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
$column = 2;
foreach ($data as $k => $item) {
$sheet->setCellValue("A$column", $item['product_id']);
$sheet->setCellValue("B$column", $item['store_name']);
$sheet->setCellValue("C$column", $item['purchase_price']);
$sheet->setCellValue("D$column", $item['purchase_lv']);
$sheet->setCellValue("E$column", $item['purchase']);
$sheet->setCellValue("F$column", $item['cost_lv']);
$sheet->setCellValue("I$column", $item['cost']);
$sheet->setCellValue("J$column", $item['vip_lv']);
$sheet->setCellValue("G$column", $item['vip_price']);
$sheet->setCellValue("H$column", $item['price_lv']);
$sheet->setCellValue("K$column", $item['price']);
$sheet->setCellValue("L$column", $item['create_time']);
$column++;
}
// 定义线框样式
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN, // 线框样式
'color' => ['argb' => '000000'], // 线框颜色
],
],
];
$sheet->getStyle('A1:L' . $column)->applyFromArray($styleArray);
$writer = new Xlsx($spreadsheet);
$url = '/export/' . "商品价格更改列表 " . 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;
}
}