Here is the solution which I have used in my custom module.
In this file add below line on code:
app/code/Custom/Module/etc/di.xml
<preference for="Magento\Ui\Model\Export\ConvertToCsv" type="Custom\Module\Model\Export\ConvertToCsv" />
Now create a new file and read the comment which I have mentioned in my code which you have replace as per your requirement.
app/code/Custom/Module/Model/Export/ConvertToCsv.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Custom\Module\Model\Export;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Ui\Component\MassAction\Filter;
/**
* Class ConvertToCsv
*/
class ConvertToCsv extends \Magento\Ui\Model\Export\ConvertToCsv
{
/**
* @var DirectoryList
*/
protected $directory;
/**
* @var MetadataProvider
*/
protected $metadataProvider;
/**
* @var int|null
*/
protected $pageSize = null;
/**
* @var Filter
*/
protected $filter;
/**
* @param Filesystem $filesystem
* @param Filter $filter
* @param MetadataProvider $metadataProvider
* @param int $pageSize
* @throws FileSystemException
*/
public function __construct(
Filesystem $filesystem,
Filter $filter,
\Magento\Ui\Model\Export\MetadataProvider $metadataProvider,
$pageSize = 200
) {
$this->filter = $filter;
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->metadataProvider = $metadataProvider;
$this->pageSize = $pageSize;
parent::__construct($filesystem, $filter, $metadataProvider, $pageSize);
}
/**
* Returns CSV file
*
* @return array
* @throws LocalizedException
*/
public function getCsvFile()
{
$component = $this->filter->getComponent();
$name = md5(microtime());
$file = 'export/'. $component->getName() . $name . '.csv';
$this->filter->prepareComponent($component);
$this->filter->applySelectionOnTargetProvider();
$dataProvider = $component->getContext()->getDataProvider();
$fields = $this->metadataProvider->getFields($component);
$options = $this->metadataProvider->getOptions();
$this->directory->create('export');
$stream = $this->directory->openFile($file, 'w+');
$stream->lock();
$stream->writeCsv($this->metadataProvider->getHeaders($component));
$i = 1;
$searchCriteria = $dataProvider->getSearchCriteria()
->setCurrentPage($i)
->setPageSize($this->pageSize);
$totalCount = (int) $dataProvider->getSearchResult()->getTotalCount();
while ($totalCount > 0) {
$items = $dataProvider->getSearchResult()->getItems();
foreach ($items as $item) {
if($component->getName() == 'custom_item_listing') { // replace this listing by your listing.xml file name
$item->setCustomValue('test'); // this will be your custom attribute/column which needs to be exported as per your requirement.
}
$this->metadataProvider->convertDate($item, $component->getName());
$stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
}
$searchCriteria->setCurrentPage(++$i);
$totalCount = $totalCount - $this->pageSize;
}
$stream->unlock();
$stream->close();
return [
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
];
}
}
This solution is working fine for me. Hope this will work for you as well.
Thank you.