How to customize ExportButton Grid in Magento 2

Today, we are going to view how to customize the export grid in Magento 2.
In the Magento 2 configuration, ExportButton Component is responsible for export grid data to specified data format (cvs, xml, and so on).

For this Magento 2 tutorial, we are going to use a Module example called “StockAlert”

To enable ExportButton Component we need to add exportButton element with specified selectProvider item into listing configuration file (listing.xml), inside
… / app / code / [vendor] / [module] / view / adminhtml / ui_component / [vendor] _ [module] _ [referencia] _ listing.xml

interactiv4_stockalert_report_listing.interactiv4_stockalert_report_listing.spinner_columns.ids
csv
CSV
interactiv4_stockalert/export/gridToCsv
xml
Excel XML
interactiv4_stockalert/export/gridToXml

Let’s do a quick code review:

Inside the tag exist a tag where is setted the select Provider

interactiv4_stockalert_report_listing.interactiv4_stockalert_report_listing.spinner_columns.ids

In the next tag set the controllers where customize output format is (CSV/XML/…)

csv
CSV
interactiv4_stockalert/export/gridToCsv
xml
Excel XML
interactiv4_stockalert/export/gridToXml

In tag interactiv4_stockalert/export/gridToCsv tag is called the controller, which implements the model “ConvertToCsv.php”

The controller … / app / code / [vendor] / [module] / Controller / Adminhtml / Export / GridToCsv.php

<?php>
declare(strict_types=1);
/**
* @author Interactiv4 Team
* @copyright Copyright (c) Interactiv4 (https://www.interactiv4.com)
*/
namespace Interactiv4\StockAlert\Controller\Adminhtml\Export;
use Interactiv4\StockAlert\Model\Export\ConvertToCsv;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Response\Http\FileFactory;
/**
* Class Render
*/
class GridToCsv extends Action
{
/**
* @var ConvertToCsv
*/
protected $converter;
.
.
.

And the model … / app / code / [vendor] / [module] / Model / Export / ConvertToCsv.php

<?php
declare(strict_types=1);
/**
* @author Interactiv4 Team
* @copyright Copyright (c) Interactiv4 (https://www.interactiv4.com)
*/
namespace Interactiv4\StockAlert\Model\Export;
use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Convert\ExcelFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Ui\Model\Export\MetadataProvider;
use Magento\Ui\Model\Export\SearchResultIteratorFactory;
/**
* Class ConvertToXls
*/
class ConvertToCsv
{
/**
* @var WriteInterface
*/
protected $directory;
.
.
.

Then you can implement into this model the code to customize the export format file.