mkm 99730d82ad refactor(xlsx): 优化多个报表的导出功能
- 新增创建导出目录的逻辑,确保目录存在
- 修改文件保存路径,使用动态创建的目录
- 优化 URL 生成逻辑,使其与实际文件路径一致
- 引入异常处理,当目录创建失败时抛出异常
2025-01-01 10:47:19 +08:00

142 lines
5.5 KiB
PHP

<?php
namespace app\common\service\xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 采购信息
*/
class OrderInfo
{
public function export($data,$order,$other_data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 合并单元格A1到K1
$sheet->mergeCells('A1:R1');
$sheet->mergeCells('B2:C2');
$sheet->mergeCells('E2:F2');
$sheet->mergeCells('G2:H2');
$sheet->mergeCells('I2:L2');
$sheet->mergeCells('M2:N2');
$sheet->mergeCells('O2:P2');
$sheet->setCellValue('A1', '采 购 信 息');
$sheet->setCellValue('A2', '姓名:');
$sheet->setCellValue('B2', $other_data->nickname??'');
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('Q')->setAutoSize(true);
$sheet->setCellValue('D2', '电话:');
$sheet->setCellValue('E2', $other_data->phone??'');
$sheet->setCellValue('G2', '收货地址:');
$sheet->setCellValue('I2', $other_data->address??'');
$sheet->setCellValue('M2', '订单编号:');
$sheet->setCellValue('O2', $order['order_id']??'');
$sheet->setCellValue('A3', '编号');
$sheet->setCellValue('B3', '品名');
$sheet->setCellValue('C3', '毛重');
$sheet->setCellValue('D3', '净重(约)');
$sheet->setCellValue('E3', '单位');
$sheet->setCellValue('F3', '采购单价');
$sheet->setCellValue('G3', '下单数量');
$sheet->setCellValue('H3', '实收量');
$sheet->setCellValue('I3', '采购金额');
$sheet->setCellValue('J3', '结算模式');
$sheet->setCellValue('K3', '包装');
$sheet->setCellValue('L3', '押金');
$sheet->setCellValue('M3', '型号');
$sheet->setCellValue('N3', '售后');
$sheet->setCellValue('O3', '损耗');
$sheet->setCellValue('P3', '供应商');
$sheet->setCellValue('Q3', '备注');
$sheet->setCellValue('R3', '采购人');
// 设置默认的单元格样式
$defaultStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
];
// 应用默认样式到整个工作表
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
foreach ($data as $k => $v) {
$sheet->setCellValue('A' . ($k + 4), $v['product_id']);
$sheet->setCellValue('B' . ($k + 4), $v['store_name']);
$sheet->setCellValue('C' . ($k + 4), $v['gross_weight']);
$sheet->setCellValue('D' . ($k + 4), $v['net_weight']);
$sheet->setCellValue('E' . ($k + 4), $v['unit_name']);
$sheet->setCellValue('F' . ($k + 4), $v['price']);
$sheet->setCellValue('G' . ($k + 4), $v['need_num']);
$sheet->setCellValue('H' . ($k + 4), $v['buyer_nums']);
$sheet->setCellValue('I' . ($k + 4), $v['total_price']);
$sheet->setCellValue('J' . ($k + 4), '');
$sheet->setCellValue('K' . ($k + 4), $v['package']);
$sheet->setCellValue('L' . ($k + 4), $v['deposit']);
$sheet->setCellValue('M' . ($k + 4), $v['marques']);
$sheet->setCellValue('N' . ($k + 4), $v['after_sales']);
$sheet->setCellValue('O' . ($k + 4), $v['loss']);
$sheet->setCellValue('P' . ($k + 4), '');
$sheet->setCellValue('Q' . ($k + 4), $v['mark']);
$sheet->setCellValue('R' . ($k + 4), $v['buyer_name']);
}
$count = count($data);
$sheet->setCellValue('A' . ($count + 5),'合计');
$sheet->setCellValue('C' . ($count + 5),$count);
$sheet->mergeCells('A' . ($count + 6).':B' . ($count + 6));
$sheet->setCellValue('A' . ($count + 6),'采购员接单确认:');
$sheet->mergeCells('E' . ($count + 6).':G' . ($count + 6));
$sheet->setCellValue('E' . ($count + 6),'数据入库时间:');
$sheet->mergeCells('M' . ($count + 6).':N' . ($count + 6));
$sheet->setCellValue('M' . ($count + 6),'采购员录入签字:');
// 设置单元格的样式
$styleArray = [
'font' => [
'bold' => true,
'size' => 28,
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
// 定义线框样式
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN, // 线框样式
'color' => ['argb' => '000000'], // 线框颜色
],
],
];
$sheet->getStyle('A1:R' . ($count + 6))->applyFromArray($styleArray);
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .'采购信息-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/'.date('Y-m') . '/' .'采购信息-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;
}
}