I have custom module and implemented custom grid. Everything working fine as expected. only issue with while export data, i am getting first row as empty. From second row on words showing column header and from third row on words showing table data. All table records are showing in grid and csv file. There is no data miss. Only issue with export csv file first row have empty. I am using UI component for admin grid implementation and using export tags for export i.e.

  • Hi @RakeshDonga Thank you for your reply. I didn't written any extra code for export functionality, just i added export button in ui component file as per magento ui compoent standards.
    – balu
    Aug 1, 2019 at 10:37
  • this code is working for you? Aug 1, 2019 at 11:01
  • @RakeshDonga I just implemented like that code, lastly I received a solution. I have a dropdown ui componet field let's say like months[dropdown]. Following this link[ https:/stackoverflow.com/questions/48836155/magento-2-custom-import-remove-import-behaviour] now export work fine, implies first row not coming empty. Thank you for your precious moment and answers.
    – balu
    Aug 1, 2019 at 12:06
  • @RakeshDonga sure, you can............
    – balu
    Aug 1, 2019 at 12:14
  • i have updated my answer check it Aug 1, 2019 at 12:18

1 Answer 1


First create your UI Component

<listingToolbar name="listing_top">
        <exportButton class="Magento\Ui\Component\ExportButton" component="Magento_Ui/js/grid/export" displayArea="dataGridActions">
                    <option name="cvs" xsi:type="array">
                        <item name="value" xsi:type="string">csv</item>
                        <item name="label" xsi:type="string" translate="true">CSV</item>
                        <item name="url" xsi:type="string">controller/results/export</item>
                    <option name="xml" xsi:type="array">
                        <item name="value" xsi:type="string">xml</item>
                        <item name="label" xsi:type="string" translate="true">Excel XML</item>
                        <item name="url" xsi:type="string">skininc/results/export</item>

Next create your controller for skininc/results/export

 * @category Magento 2 Module
 * @package  Overdosedigital\Frontendflow
 * @author   Don Nuwinda
namespace MyModule\Frontendflow\Controller\Adminhtml\Results;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Ui\Model\Export\ConvertToCsv;
use Magento\Framework\App\Response\Http\FileFactory;
use MyModule\Frontendflow\Model\ResourceModel\MyData\CollectionFactory;

class Export extends \Magento\Backend\App\Action
     * @var \Magento\Backend\Model\View\Result\ForwardFactory
    protected $resultForwardFactory;

     * Massactions filter
     * @var Filter
    protected $filter;

     * @var MetadataProvider
    protected $metadataProvider;
     * @var WriteInterface
    protected $directory;
     * @var ConvertToCsv
    protected $converter;
     * @var FileFactory
    protected $fileFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
        Filter $filter,
        Filesystem $filesystem,
        ConvertToCsv $converter,
        FileFactory $fileFactory,
        \Magento\Ui\Model\Export\MetadataProvider $metadataProvider,
        \MyModule\Frontendflow\Model\ResourceModel\MyData $resource,
        CollectionFactory $collectionFactory
        ) {
            $this->resources = $resource;
            $this->filter = $filter;
            $this->_connection = $this->resources->getConnection();
            $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
            $this->metadataProvider = $metadataProvider;
            $this->converter = $converter;
            $this->fileFactory = $fileFactory;
            $this->resultForwardFactory = $resultForwardFactory;
            $this->collectionFactory = $collectionFactory;

     * export.
     * @return \Magento\Backend\Model\View\Result\Forward
    public function execute()
        $collection = $this->filter->getCollection($this->collectionFactory->create());

        $component = $this->filter->getComponent();
        $dataProvider = $component->getContext()->getDataProvider();
        $dataProvider->setLimit(0, false);
        $ids = [];

        foreach ($collection as $document) {
            $ids[] = (int)$document->getId();

        $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
        $fields = $this->metadataProvider->getFields($component);
        $options = $this->metadataProvider->getOptions();
        $name = md5(microtime());
        $file = 'export/'. $component->getName() . $name . '.csv';
        $stream = $this->directory->openFile($file, 'w+');
        foreach ($searchResult->getItems() as $document) {
            if( in_array( $document->getId(), $ids ) ) {
                $this->metadataProvider->convertDate($document, $component->getName());
                $stream->writeCsv($this->metadataProvider->getRowData($document, $fields, $options));
        return $this->fileFactory->create('export.csv', [
            'type' => 'filename',
            'value' => $file,
            'rm' => true  // can delete file after use
        ], 'var');
  • Thankyou, working fine.
    – balu
    Aug 1, 2019 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.