FormAnswerSheet.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Exports\Sheets;
  3. use App\Models\Fpdx\FormAnswerModel;
  4. use Maatwebsite\Excel\Concerns\FromQuery;
  5. use Maatwebsite\Excel\Concerns\WithHeadings;
  6. use Maatwebsite\Excel\Concerns\WithMapping;
  7. use Maatwebsite\Excel\Concerns\WithTitle;
  8. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  9. class FormAnswerSheet implements FromQuery, WithTitle, WithMapping, WithHeadings
  10. {
  11. private $question;
  12. public function __construct($question)
  13. {
  14. $this->question = $question;
  15. }
  16. public function headings(): array
  17. {
  18. return [
  19. '#',
  20. 'User',
  21. 'Type',
  22. 'Answer',
  23. 'Date'
  24. ];
  25. }
  26. public function map($answer): array
  27. {
  28. return [
  29. '#',
  30. $answer->uid,
  31. $answer->type,
  32. $answer->answer,
  33. $answer->created_at,
  34. ];
  35. }
  36. public function query()
  37. {
  38. return FormAnswerModel::query()->where('question_id', $this->question->id);
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function title(): string
  44. {
  45. return sprintf('第%s题', $this->question->sort);
  46. }
  47. public function columnFormats(): array
  48. {
  49. return [
  50. 'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
  51. ];
  52. }
  53. }