From 7f4327672069f90695f5d614ab56cc4d056236cd Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Wed, 9 Oct 2024 15:32:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE=E5=8D=95?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除了每个条目后的强制分页,只在非最后一个条目时添加分页 - 修正了文件名中的日期格式,使其与实际导出时间一致 - 代码格式调整,提高可读性和维护性 --- app/common/service/xlsx/Beforehand.php | 48 ++++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/app/common/service/xlsx/Beforehand.php b/app/common/service/xlsx/Beforehand.php index 0c6958c21..9c9e34e58 100644 --- a/app/common/service/xlsx/Beforehand.php +++ b/app/common/service/xlsx/Beforehand.php @@ -7,42 +7,44 @@ use PhpOffice\PhpWord\Shared\Converter; class Beforehand { - public function export($data,$system_store) + public function export($data, $system_store) { - // 创建一个新的PHPWord文档 - $phpWord = new PhpWord(); + // 创建一个新的PHPWord文档 + $phpWord = new PhpWord(); - // 添加一个节 - $section = $phpWord->addSection(array( - 'pageSizeW' => Converter::cmToTwip(4), // 宽度转换为twips - 'pageSizeH' => Converter::cmToTwip(2), // 高度转换为twips - 'marginLeft' => Converter::cmToTwip(0.2), - 'marginRight' => Converter::cmToTwip(0), - 'marginTop' => Converter::cmToTwip(0.2), - 'marginBottom' => Converter::cmToTwip(0), - )); - $fontStyle = ['name' => 'Arial', 'size' => 10, 'bold' => true]; - $fontStyle1 = ['name' => 'Arial', 'size' => 8, 'bold' => true]; - $fontStyle2 = ['name' => 'Arial', 'size' => 8, 'bold' => true]; - $fontStyle3 = ['name' => 'Arial', 'size' => 8, 'bold' => true]; + // 添加一个节 + $section = $phpWord->addSection(array( + 'pageSizeW' => Converter::cmToTwip(4), // 宽度转换为twips + 'pageSizeH' => Converter::cmToTwip(2), // 高度转换为twips + 'marginLeft' => Converter::cmToTwip(0.2), + 'marginRight' => Converter::cmToTwip(0), + 'marginTop' => Converter::cmToTwip(0.2), + 'marginBottom' => Converter::cmToTwip(0), + )); + $fontStyle = ['name' => 'Arial', 'size' => 10, 'bold' => true]; + $fontStyle1 = ['name' => 'Arial', 'size' => 8, 'bold' => true]; + $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); - $section->addPageBreak(); - + 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; } }