2

I would like to implement export catalog products functionality from admin end from custom module.

I have followed below steps for displaying export button at admin end catalog > products but unable to export products data in the form of csv/xml/xls.

  1. Created custom module Vendor_Employee.

  2. app/code/Vendor/Employee/view/adminhtml/ui_component/product_listing.xml

    <?xml version="1.0" encoding="UTF-8"?>       
    <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">product_listing.product_listing_data_source</item>
        </item>
    </argument>
    <settings>
        <spinner>product_columns</spinner>
        <deps>
            <dep>product_listing.product_listing_data_source</dep>
        </deps>
    </settings>
    <dataSource name="product_listing_data_source" component="Magento_Ui/js/grid/provider">
        <settings>
            <storageConfig>
                <param name="dataScope" xsi:type="string">filters.store_id</param>
            </storageConfig>
            <updateUrl path="mui/index/render"/>
        </settings>
        <aclResource>Magento_Catalog::products</aclResource>
        <dataProvider class="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" name="product_listing_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <listingToolbar name="listing_top">
        <settings>
            <sticky>true</sticky>
        </settings>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters">
            <filterSelect name="store_id" provider="${ $.parentName }">
                <settings>
                    <options class="Magento\Store\Ui\Component\Listing\Column\Store\Options"/>
                    <caption translate="true">All Store Views</caption>
                    <label translate="true">Store View</label>
                    <dataScope>store_id</dataScope>
                </settings>
            </filterSelect>
        </filters>
        <massaction name="listing_massaction"
                    component="Magento_Ui/js/grid/tree-massactions"
                    class="\Magento\Catalog\Ui\Component\Product\MassAction">
            <action name="delete">
                <settings>
                    <confirm>
                        <message translate="true">Delete selected items?</message>
                        <title translate="true">Delete items</title>
                    </confirm>
                    <url path="catalog/product/massDelete"/>
                    <type>delete</type>
                    <label translate="true">Delete1</label>
                </settings>
            </action>
            <action name="status">
                <settings>
                    <type>status</type>
                    <label translate="true">Change status1</label>
                    <actions>
                        <action name="0">
                            <type>enable</type>
                            <label translate="true">Enable</label>
                            <url path="catalog/product/massStatus">
                                <param name="status">1</param>
                            </url>
                        </action>
                        <action name="1">
                            <type>disable</type>
                            <label translate="true">Disable1</label>
                            <url path="catalog/product/massStatus">
                                <param name="status">2</param>
                            </url>
                        </action>
                    </actions>
                </settings>
            </action>
            <action name="attributes">
                <settings>
                    <url path="catalog/product_action_attribute/edit"/>
                    <type>attributes</type>
                    <label translate="true">Update attributes</label>
                </settings>
            </action>
        </massaction>
        <paging name="listing_paging"/>
    
        <!-- Added Export Button Here -->
    
        <exportButton class="Magento\Ui\Component\ExportButton">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="options" xsi:type="array">
                        <item 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">employee/export/gridToCsv</item>
                        </item>
                        <item 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">employee/export/gridToXml</item>
                        </item>
                        <item name="xls" xsi:type="array">
                            <item name="value" xsi:type="string">xls</item>
                            <item name="label" xsi:type="string" translate="true">Excel XLS</item>
                            <item name="url" xsi:type="string">employee/export/gridToXls</item>
                        </item>
                    </item>
                </item>
            </argument>
        </exportButton>
     <!-- Added Export Button Here -->
    
    </listingToolbar>
    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <settings>
            <childDefaults>
                <param name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">product_listing.product_listing.product_columns.actions</item>
                    <item name="target" xsi:type="string">applyAction</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">edit</item>
                        <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                    </item>
                </param>
            </childDefaults>
        </settings>
        <selectionsColumn name="ids" sortOrder="0">
            <settings>
                <indexField>entity_id</indexField>
            </settings>
        </selectionsColumn>
        <column name="entity_id" sortOrder="10">
            <settings>
                <filter>textRange</filter>
                <label translate="true">ID</label>
                <sorting>asc</sorting>
            </settings>
        </column>
        <column name="thumbnail" class="Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail" component="Magento_Ui/js/grid/columns/thumbnail" sortOrder="20">
            <settings>
                <altField>name</altField>
                <hasPreview>1</hasPreview>
                <addField>true</addField>
                <label translate="true">Thumbnail</label>
                <sortable>false</sortable>
            </settings>
        </column>
        <column name="name" sortOrder="30">
            <settings>
                <addField>true</addField>
                <filter>text</filter>
                <label translate="true">Name</label>
            </settings>
        </column>
        <column name="type_id" component="Magento_Ui/js/grid/columns/select" sortOrder="40">
            <settings>
                <options class="Magento\Catalog\Model\Product\Type"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Type</label>
            </settings>
        </column>
        <column name="attribute_set_id" component="Magento_Ui/js/grid/columns/select" sortOrder="50">
            <settings>
                <options class="Magento\Catalog\Model\Product\AttributeSet\Options"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Attribute Set</label>
            </settings>
        </column>
        <column name="sku" sortOrder="60">
            <settings>
                <filter>text</filter>
                <label translate="true">SKU</label>
            </settings>
        </column>
        <column name="price" class="Magento\Catalog\Ui\Component\Listing\Columns\Price" sortOrder="70">
            <settings>
                <addField>true</addField>
                <filter>textRange</filter>
                <label translate="true">Price</label>
            </settings>
        </column>
        <column name="visibility" component="Magento_Ui/js/grid/columns/select" sortOrder="80">
            <settings>
                <addField>true</addField>
                <options class="Magento\Catalog\Model\Product\Visibility"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Visibility</label>
            </settings>
        </column>
        <column name="status" component="Magento_Ui/js/grid/columns/select" sortOrder="90">
            <settings>
                <addField>true</addField>
                <options class="Magento\Catalog\Model\Product\Attribute\Source\Status"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Status</label>
            </settings>
        </column>
        <column name="websites" class="Magento\Catalog\Ui\Component\Listing\Columns\Websites" sortOrder="100">
            <settings>
                <addField>true</addField>
                <options class="Magento\Store\Model\ResourceModel\Website\Collection"/>
                <dataType>text</dataType>
                <label translate="true">Websites</label>
            </settings>
        </column>
        <actionsColumn name="actions" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions" sortOrder="200">
            <settings>
                <indexField>entity_id</indexField>
            </settings>
        </actionsColumn>
    </columns>
    

  3. app/code/Vendor/Employee/Controller/Adminhtml/Export/GridToCsv.php

    <?php
    namespace Vendor\Employee\Controller\Adminhtml\Export;
    use Magento\Backend\App\Action;
    use Magento\Backend\App\Action\Context;
    use Magento\Framework\App\ResponseInterface;
    use Magento\Framework\App\Filesystem\DirectoryList;
    use Magento\Framework\App\Response\Http\FileFactory;
    class gridToCsv extends Action
    {
    
    public function execute()
    {
            //csv
            $this->_view->loadLayout(false);
            $fileName = 'catalog_products.csv';
            $exportBlock = $this->_view->getLayout()->createBlock('Magento\Catalog\Block\Adminhtml\Product\Grid');
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $this->_fileFactory = $objectManager->create('Magento\Framework\App\Response\Http\FileFactory');
            return $this->_fileFactory->create(
                $fileName,
                $exportBlock->getCsvFile(),
                DirectoryList::VAR_DIR
            );
    
    }
    }
    

enter image description here

Here i am able to export only few columns but not all Please advise?

  • working or not need a code – Rk Rathod Aug 13 at 14:36
0

I assume that in grid/listing xml you need to add "selectProvider" item to exportButton node. In your case:

<exportButton class="Magento\Ui\Component\ExportButton">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="options" xsi:type="array">
                <item name="selectProvider" xsi:type="string">
                    product_listing.product_listing.product_columns.ids
                </item>
                <item 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">employee/export/gridToCsv</item>
                </item>
                <item 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">employee/export/gridToXml</item>
                </item>
                <item name="xls" xsi:type="array">
                    <item name="value" xsi:type="string">xls</item>
                    <item name="label" xsi:type="string" translate="true">Excel XLS</item>
                    <item name="url" xsi:type="string">employee/export/gridToXls</item>
                </item>
            </item>
        </item>
    </argument>
</exportButton>

The controller seems to be okay, but to be sure check the OOB exporter :

\Magento\Ui\Controller\Adminhtml\Export\GridToCsv

If the problem is in exporter, try to extend from the OOB one.

0

Below Is a Default Class it only Export that Data which are show in your Admin Grid not Export your whole data

<exportButton class="Magento\Ui\Component\ExportButton">

If you want all Data you have create a Custom Export Button that export your all data.

edit in your Ui-Component file where Admin Grid Show

[vendor_name] \ [module_name] \view\adminhtml\ui_component

Add this code to add Button into Admin Grid

Ui-ComponentFIleName.xml

<item name="buttons" xsi:type="array">

    <item name="import" xsi:type="array">
        <item name="name" xsi:type="string">import</item>
        <item name="label" xsi:type="string" translate="true">Import</item>
        <item name="class" xsi:type="string">secondary</item>
        <item name="url" xsi:type="string">*/*/importdata</item>
        <item name="sortOrder" xsi:type="number">20</item>
    </item>

    <item name="export" xsi:type="array">
        <item name="name" xsi:type="string">export</item>
        <item name="label" xsi:type="string" translate="true">Export</item>
        <item name="class" xsi:type="string">secondary</item>
        <item name="url" xsi:type="string">*/*/exportdata</item>
        <item name="sortOrder" xsi:type="number">30</item>
    </item>

</item>

Now create a File that Export or Import Data. File Name must be same as you define in Ui-Component file

  <item name="url" xsi:type="string">*/*/exportdata</item>

  <item name="url" xsi:type="string">*/*/importdata</item>

File path must be like this

[vendor_name] \ [module_name] \ Controller \ Adminhtml \ [Controller_name] \ Exportdata.php

Exportdata.php

<?php

namespace [vendor_name]\[module_name]\Controller\Adminhtml\[Controller_name];

use Magento\Framework\App\Filesystem\DirectoryList;


class Exportdata extends \Magento\Backend\App\Action
{
    protected $uploaderFactory;

    protected $_locationFactory; 

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Filesystem $filesystem,
        \[vendor_name]\[module_name]\Model\locatorFactory $locationFactory // This is returns Collaction of Data

    ) {
       parent::__construct($context);
       $this->_fileFactory = $fileFactory;
       $this->_locationFactory = $locationFactory;
       $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); // VAR Directory Path
       parent::__construct($context);
    }

    public function execute()
    {   
        $name = date('m-d-Y-H-i-s');
        $filepath = 'export/export-data-' .$name. '.csv'; // at Directory path Create a Folder Export and FIle
        $this->directory->create('export');

        $stream = $this->directory->openFile($filepath, 'w+');
        $stream->lock();

        //column name dispay in your CSV 

        $columns = ['Col-1-name','Col-2-name','Col-3-name','Col-4-name','Col-5-name','Col-6-name','Col-7-name','Col-8-name','Col-9-name',];

            foreach ($columns as $column) 
            {
                $header[] = $column; //storecolumn in Header array
            }

        $stream->writeCsv($header);

        $location = $this->_locationFactory->create();
        $location_collection = $location->getCollection(); // get Collection of Table data 

        foreach($location_collection as $item){

            $itemData = [];

            // column name must same as in your Database Table 

            $itemData[] = $item->getData('col-1-name');
            $itemData[] = $item->getData('col-2-name');
            $itemData[] = $item->getData('col-3-name');
            $itemData[] = $item->getData('col-4-name');
            $itemData[] = $item->getData('col-5-name');
            $itemData[] = $item->getData('col-6-name');
            $itemData[] = $item->getData('col-7-name');
            $itemData[] = $item->getData('col-8-name');
            $itemData[] = $item->getData('col-9-name');

            $stream->writeCsv($itemData);

        }

        $content = [];
        $content['type'] = 'filename'; // must keep filename
        $content['value'] = $filepath;
        $content['rm'] = '1'; //remove csv from var folder

        $csvfilename = 'locator-import-'.$name.'.csv';
        return $this->_fileFactory->create($csvfilename, $content, DirectoryList::VAR_DIR);

    }


}

Now you can Click on the Export Button and See your .csv file Downloaded below.

I Hope This Helps You.

  • this is only for Export Data in .csv format only. – Msquare Sep 29 at 9:23
  • if anyone have a any problem related to above code so ask me here . I will give you a best solution for that problem – Msquare Sep 29 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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