In this tutorial, Today I will explain to how to create CSV and download programmatically in Magento 2. Whenever we need to import or export data we saved data into CSV. If you want to import & export data using CSV and download programmatically then, you need to follow the below steps for that.
You need to use \Magento\Framework\App\Response\Http\FileFactory this class to create a CSV file and download the file as per Magento standard way.
You may also like this :
- How to Download File Programmatically in Magento 2
- How to Create Excel File Programmatically in Magento 2
I added code inside one controller file. You can add in any files where you want to add this code.
1) Let’s assume that you have created simple module. Now, create controller for create csv file and download at app/code/RH/Helloworld/Controller/Index/ExportCsv.php and paste the below code :
<?php
/**
* Created By : Rohan Hapani
*/
namespace RH\Helloworld\Controller\Index;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ResponseInterface;
class ExportCsv extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Response\Http\FileFactory
*/
protected $fileFactory;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;
/**
* @var \Magento\Framework\View\Result\LayoutFactory
*/
protected $resultLayoutFactory;
/**
* @var \Magento\Framework\File\Csv
*/
protected $csvProcessor;
/**
* @var \Magento\Framework\App\Filesystem\DirectoryList
*/
protected $directoryList;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
* @param \Magento\Framework\File\Csv $csvProcessor
* @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
\Magento\Framework\File\Csv $csvProcessor,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList
) {
$this->fileFactory = $fileFactory;
$this->productFactory = $productFactory;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->csvProcessor = $csvProcessor;
$this->directoryList = $directoryList;
parent::__construct($context);
}
/**
* CSV Create and Download
*
* @return ResponseInterface
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function execute()
{
/** Add yout header name here */
$content[] = [
'entity_id' => __('Entity ID'),
'attribute_set_id' => __('Attribute Set ID'),
'type_id' => __('Type ID'),
'sku' => __('Sku'),
'required_options' => __('Required Options'),
'created_at' => __('Created At'),
'updated_at' => __('Updated At'),
];
$resultLayout = $this->resultLayoutFactory->create();
$product = $this->productFactory->create()->getCollection();
$collection = $this->productFactory->create()->getCollection();
$fileName = 'rohan_hapani_file.csv'; // Add Your CSV File name
$filePath = $this->directoryList->getPath(DirectoryList::MEDIA) . "/" . $fileName;
while ($product = $collection->fetchItem()) {
$content[] = [
$product->getEntityId(),
$product->getAttributeSetId(),
$product->getTypeId(),
$product->getSku(),
$product->getRequiredOptions(),
$product->getCreatedAt(),
$product->getUpdatedAt()
];
}
$this->csvProcessor->setEnclosure('"')->setDelimiter(',')->saveData($filePath, $content);
return $this->fileFactory->create(
$fileName,
[
'type' => "filename",
'value' => $fileName,
'rm' => true, // True => File will be remove from directory after download.
],
DirectoryList::MEDIA,
'text/csv',
null
);
}
}
Now, execute this above controller and csv file will be download successfully !! You can develop import & export data in CSV by this code also.
That’s it !!!
Output :
I hope this blog is easy to understand how to create CSV and download programmatically in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with a proper solution.
Stay Safe and Stay Connected !!