customerSession = $customerSession; $this->labRepository = $labRepository; $this->redirectFactory = $redirectFactory; $this->messageManager = $messageManager; $this->labDownloadHelper = $labDownloadHelper; $this->fileFactory = $fileFactory; $this->resultRawFactory = $resultRawFactory; $this->logger = $logger; } public function execute() { $labs = $this->labRepository->getByDealerId($this->customerSession->getCustomerId()); if (!$labs->count()) { $this->messageManager->addErrorMessage('您暂无可下载的Lab'); return $this->redirectFactory->create()->setPath('customer/account/index/'); } try { return $this->fileFactory->create('export-'.date('YmdHis').'.xlsx', $this->labDownloadHelper->write($labs), 'media'); } catch (\Exception | \PhpOffice\PhpSpreadsheet\Exception $e) { $this->logger->critical('Lab下载失败, 原因:'.$e->getMessage()); $this->messageManager->addErrorMessage('Lab下载失败'); return $this->redirectFactory->create()->setPath('customer/account/index/'); } } }