Magento2 : Custom Export

Go to your block file add bellow line in _prepareColumns() function.
protected function _prepareColumns()
{
 $this->addExportType('modulename/*/controllerActionName', __('CSV'));
}
It will display export button.
For download CSV file,Create controller with given name.
namespace NameSpace\ModuleName\Controller\Adminhtml\FolderName;
use NameSpace\ModuleName\Controller\Adminhtml\FolderName;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action\Context;

class controllerActionName extends \Magento\Backend\App\Action 
{
   
    protected $_resultLayoutFactory;
    protected $_layoutPage;
    protected $_resultPageFactory;
    protected $_fileFactory;
    
    public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory, 
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,        
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory) 
    {
        parent::__construct($context);
        $this->_resultPageFactory = $resultPageFactory;
        $this->_fileFactory = $fileFactory;
        $this->_resultLayoutFactory = $resultLayoutFactory;
    }
    public function execute() 
    {
       $this->_setPageData();
       return $this->getLayoutPage(); 
    }
    
    public function getLayoutPage() {
        if (is_null($this->_layoutPage)) {
            $this->_layoutPage = $this->_resultLayoutFactory->create();
            
        }
        return $this->_layoutPage;
    }
    
    protected function _setPageData() {
        $layoutPage = $this->getLayoutPage();
        return $this;
    }

    public function execute()
    {
        $fileName = 'filename.csv';
        $resultPage = $this->_resultPageFactory->create();
       // $content = $resultPage->getLayout()->createBlock('path_grid_to_export')->getCsv();  // Default grid data will downloaded
        $content = $resultPage->getLayout()->createBlock('path_grid_to_export')->downloadCsv(); // For custom Modification..
        return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
    } 
}
Now got to same block file define function downloadCsv().Here you get a collection and column name.
public function downloadCsv()
    {
        $csv = '';
        $this->_isExport = true;
        $this->_prepareGrid();
        $this->getCollection()->getSelect()->limit();
        $this->getCollection()->setPageSize(0);
        $this->getCollection()->load();
        $this->_afterLoadCollection();

        $data = [];
        foreach ($this->getColumns() as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . $column->getExportHeader() . '"';
            }
        }

        $csv .= implode(',', $data) . "\n";

        foreach($this->getCollection()->getData() as $key=>$val)
        {
            $csv .= implode(',', $val) . "\n";
        }
        
        return $csv;
    }

Comments