* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Repositories\Models\Base\Admin; use App\Repositories\Models\Lab\OpenAppointment; use Illuminate\Http\Request; use PhpOffice\PhpWord\SimpleType\TblWidth; use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\TemplateProcessor; class ExampleController extends Controller { public function test(Request $request) { // dd(1); // $admin = Admin::query()->find(1); // dd($admin->isSuperAdmin); try { $id = $request->get('id', 1); $model = OpenAppointment::query()->find($id); $path = base_path('public/template/lab/template_p_1.docx'); $is = file_exists($path); // dd($is); $template = new TemplateProcessor($path); // $phpWord = new PhpWord(); // $template = $phpWord->loadTemplate($path); //初始化模板 // $template = $phpWord->loadTemplate($path); //初始化模板 $template->setValue('department', $model->department); $template->setValue('name', $model->applicant_name); $template->setValue('mobile', $model->applicant_mobile); $template->setValue('nums', $model->use_people_nums); $template->setValue('use_reason', $model->use_reason); $template->setValue('use_need', $model->use_need); if (is_array($model->use_data)) { $labs = array_unique(array_column($model->use_data, 'room_name')); $template->setValue('use_labs', arr2str($labs)); $template->setValue('lab_nums', count($labs)); $table = new Table(['borderSize' => 5, 'width' => 5500, 'unit' => TblWidth::AUTO, 'alignMent' => 'center']); foreach ($model->use_data as $r => $time) { if ($r == 0) { $table->addRow(); $table->addCell(3000)->addText('实验室'); $table->addCell(1500)->addText('日期'); $table->addCell(1000)->addText('节次'); } $table->addRow(); $table->addCell(3000)->addText($time['room_name']); $table->addCell(1500)->addText($time['day']); $table->addCell(1000)->addText($time['time_name']); } $template->setComplexBlock('use_times', $table); } $filename = date('Y-m-d') . '.docx'; header('Content-Type: application/vnd.ms-word'); header('Content-Disposition: attachment;filename=' . $filename); header('Cache-Control: max-age=0'); $template->saveAs('php://output');//直接下载 } catch (\Exception $exception) { dd($exception); } } }