优化标签单导出功能

- 移除了每个条目后的强制分页,只在非最后一个条目时添加分页
- 修正了文件名中的日期格式,使其与实际导出时间一致
- 代码格式调整,提高可读性和维护性
This commit is contained in:
mkm 2024-10-09 15:32:47 +08:00
parent 69b4a8cafe
commit 7f43276720

View File

@ -7,7 +7,7 @@ use PhpOffice\PhpWord\Shared\Converter;
class Beforehand
{
public function export($data,$system_store)
public function export($data, $system_store)
{
// 创建一个新的PHPWord文档
$phpWord = new PhpWord();
@ -26,23 +26,25 @@ class Beforehand
$fontStyle2 = ['name' => 'Arial', 'size' => 8, 'bold' => true];
$fontStyle3 = ['name' => 'Arial', 'size' => 8, 'bold' => true];
$count = count($data);
foreach ($data as $k => $v) {
$textRun =$section->addTextRun();
$textRun = $section->addTextRun();
$textRun->addText($v['system_store'], $fontStyle);
$textRun->addText(' '.$v['subtitle'], $fontStyle1);
$section->addText(mb_substr( $v['store_name'], 0, 16, 'UTF-8'), $fontStyle2);
$textRun->addText(' ' . $v['subtitle'], $fontStyle1);
$section->addText($v['store_name'], $fontStyle2);
$section->addText($v['address'], $fontStyle3);
if($k+1!=$count){
$section->addPageBreak();
}
}
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$url = '/export/' . date('Y-m') . '/' . $system_store.'标签单-'.date('Y-m-d H:i') . '.docx';
$url = '/export/' . date('Y-m') . '/' . $system_store . '标签单-' . date('Y-m-d H:i') . '.docx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$objWriter->save($file_path);
return getenv('APP_URL').$url;
return getenv('APP_URL') . $url;
}
}