ExampleController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Controllers\Admin;
  11. use App\Http\Controllers\Controller;
  12. use App\Repositories\Models\Base\Admin;
  13. use App\Repositories\Models\Lab\OpenAppointment;
  14. use Illuminate\Http\Request;
  15. use PhpOffice\PhpWord\SimpleType\TblWidth;
  16. use PhpOffice\PhpWord\Element\Table;
  17. use PhpOffice\PhpWord\TemplateProcessor;
  18. class ExampleController extends Controller
  19. {
  20. public function test(Request $request)
  21. {
  22. // dd(1);
  23. // $admin = Admin::query()->find(1);
  24. // dd($admin->isSuperAdmin);
  25. try {
  26. $id = $request->get('id', 1);
  27. $model = OpenAppointment::query()->find($id);
  28. $path = base_path('public/template/lab/template_p_1.docx');
  29. $is = file_exists($path);
  30. // dd($is);
  31. $template = new TemplateProcessor($path);
  32. // $phpWord = new PhpWord();
  33. // $template = $phpWord->loadTemplate($path); //初始化模板
  34. // $template = $phpWord->loadTemplate($path); //初始化模板
  35. $template->setValue('department', $model->department);
  36. $template->setValue('name', $model->applicant_name);
  37. $template->setValue('mobile', $model->applicant_mobile);
  38. $template->setValue('nums', $model->use_people_nums);
  39. $template->setValue('use_reason', $model->use_reason);
  40. $template->setValue('use_need', $model->use_need);
  41. if (is_array($model->use_data)) {
  42. $labs = array_unique(array_column($model->use_data, 'room_name'));
  43. $template->setValue('use_labs', arr2str($labs));
  44. $template->setValue('lab_nums', count($labs));
  45. $table = new Table(['borderSize' => 5, 'width' => 5500, 'unit' => TblWidth::AUTO, 'alignMent' => 'center']);
  46. foreach ($model->use_data as $r => $time) {
  47. if ($r == 0) {
  48. $table->addRow();
  49. $table->addCell(3000)->addText('实验室');
  50. $table->addCell(1500)->addText('日期');
  51. $table->addCell(1000)->addText('节次');
  52. }
  53. $table->addRow();
  54. $table->addCell(3000)->addText($time['room_name']);
  55. $table->addCell(1500)->addText($time['day']);
  56. $table->addCell(1000)->addText($time['time_name']);
  57. }
  58. $template->setComplexBlock('use_times', $table);
  59. }
  60. $filename = date('Y-m-d') . '.docx';
  61. header('Content-Type: application/vnd.ms-word');
  62. header('Content-Disposition: attachment;filename=' . $filename);
  63. header('Cache-Control: max-age=0');
  64. $template->saveAs('php://output');//直接下载
  65. } catch (\Exception $exception) {
  66. dd($exception);
  67. }
  68. }
  69. }