public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        
        try {
            
            $content = '';
            $content .= '"store_id",';
            $content .= '"name",';
            $content .= '"address",';
            $content .= '"city",';
            $content .= '"country",';
            $content .= '"postcode",';
            $content .= '"region",';
            $content .= '"email",';
            $content .= '"phone",';
            $content .= '"link",';
            $content .= '"image",';
            $content .= '"latitude",';
            $content .= '"longitude",';
            $content .= '"status",';
            $content .= '"updated_at",';
            $content .= '"created_at",';
            $content .= '"schedule",';
            $content .= '"station",';
            $content .= '"description",';
            $content .= '"intro",';
            $content .= '"details_image",';
            $content .= '"distance",';
            $content .= '"external_link"';
            $content .= "\n";

            $fileName = 'stockists_export.csv';
            $collection = $this->collectionFactory->create()->getData();
            
            foreach ($collection as $stockist) {
                array_shift($stockist); //skip the id
                $content .= implode(",", array_map([$this, 'addQuotationMarks'],$stockist));
                $content .= "\n";
            }

            return $this->fileFactory->create(
                $fileName,
                $content,
                DirectoryList::VAR_DIR
            );
            
            $this->messageManager->addSuccessMessage(__('You exported the file. It can be found in var folder or in browser downloads.'));
            $resultRedirect->setPath('stockists/stores');
            
        } catch (\Exception $e) {
            $this->messageManager->addErrorMessage(__('There was a problem exporting the data'));
            $resultRedirect->setPath('stockists/stores/export');
        }
        
        return $resultRedirect;

    }
public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        
        try {
            
            $content = '';
            $content .= '"store_id",';
            $content .= '"name",';
            $content .= '"address",';
            $content .= '"city",';
            $content .= '"country",';
            $content .= '"postcode",';
            $content .= '"region",';
            $content .= '"email",';
            $content .= '"phone",';
            $content .= '"link",';
            $content .= '"image",';
            $content .= '"latitude",';
            $content .= '"longitude",';
            $content .= '"status",';
            $content .= '"updated_at",';
            $content .= '"created_at",';
            $content .= '"schedule",';
            $content .= '"station",';
            $content .= '"description",';
            $content .= '"intro",';
            $content .= '"details_image",';
            $content .= '"distance",';
            $content .= '"external_link"';
            $content .= "\n";

            $fileName = 'stockists_export.csv';
            $collection = $this->collectionFactory->create()->getData();
            
            foreach ($collection as $stockist) {
                array_shift($stockist); //skip the id
                $content .= implode(",", array_map([$this, 'addQuotationMarks'],$stockist));
                $content .= "\n";
            }

            return $this->fileFactory->create(
                $fileName,
                $content,
                DirectoryList::VAR_DIR
            );
            
            $this->messageManager->addSuccessMessage(__('You exported the file. It can be found in var folder or in browser downloads.'));
            $resultRedirect->setPath('stockists/stores');
            
        } catch (\Exception $e) {
            $this->messageManager->addErrorMessage(__('There was a problem exporting the data'));
            $resultRedirect->setPath('stockists/stores/export');
        }
        
        return $resultRedirect;

    }
public function execute ( ) { $ resultRedirect = $ this -> resultRedirectFactory -> create ( ) ; try { $ content = '' ; $ content .= '"store_id",' ; $ content .= '"name",' ; $ content .= '"address",' ; $ content .= '"city",' ; $ content .= '"country",' ; $ content .= '"postcode",' ; $ content .= '"region",' ; $ content .= '"email",' ; $ content .= '"phone",' ; $ content .= '"link",' ; $ content .= '"image",' ; $ content .= '"latitude",' ; $ content .= '"longitude",' ; $ content .= '"status",' ; $ content .= '"updated_at",' ; $ content .= '"created_at",' ; $ content .= '"schedule",' ; $ content .= '"station",' ; $ content .= '"description",' ; $ content .= '"intro",' ; $ content .= '"details_image",' ; $ content .= '"distance",' ; $ content .= '"external_link"' ; $ content .= "\n" ; $ fileName = 'stockists_export.csv' ; $ collection = $ this -> collectionFactory -> create ( ) -> getData ( ) ; foreach ( $ collection as $ stockist ) { array_shift ( $ stockist ) ; //skip the id $ content .= implode ( "," , array_map ( [ $ this , 'addQuotationMarks' ] , $ stockist ) ) ; $ content .= "\n" ; } return $ this -> fileFactory -> create ( $ fileName , $ content , DirectoryList :: VAR_DIR ) ; $ this -> messageManager -> addSuccessMessage ( __ ( 'You exported the file. It can be found in var folder or in browser downloads.' ) ) ; $ resultRedirect -> setPath ( 'stockists/stores' ) ; } catch ( \ Exception $ e ) { $ this -> messageManager -> addErrorMessage ( __ ( 'There was a problem exporting the data' ) ) ; $ resultRedirect -> setPath ( 'stockists/stores/export' ) ; } return $ resultRedirect ; }
Export data grid to CSV format

@return ResponseInterface
Export data grid to CSV format