How to read CSV data as array pairs in Magento 2?

Read CSV file using Magento 2 to retrieve all the records from the CSV file used for fetch data of a specific entity.

You can Retrieve CSV file data as pairs by just calling Magento\Framework\File\Csv class. Csv.php class contains the getDataPairs() function to convert the first column as a key and second column as the value in the array from the CSV file.

This article is also useful when you submit the form with the CSV file as form post data, you can retrieve the CSV data by the $_FILES [‘tmp_name] Pass tmp_name from the Files Data to the getData($_FILES [‘tmp_name] ) function from the CSV Class to read each row of the file with an array of records.

Let’s assume your CSV file contains below value for the first column and a second column with comma-separated, Continue reading “How to read CSV data as array pairs in Magento 2?”

How to Create a CSV and Download Programmatically by Magento 2?

Create Export and Download CSV, Excel or Text File Programmatically in Magento 2 by FileFactory Class.

\Magento\Framework\App\Response\Http\FileFactory used to create CSV and download CSV by Magento way. create() function in FileFactory.php is used for create CSV file.

There are many other ways to download CSV files using Core PHP script but its not the best way to use within the Magento Coding standard.
You can write CSV and download a CSV file using just simple below code snippet in your controller file,

<?php
namespace Rbj\CSV\Controller\Adminhtml\Index;

use Magento\Framework\App\Filesystem\DirectoryList;

class Export extends \Magento\Backend\App\Action
{
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Filesystem $filesystem
    ) {
        $this->_fileFactory = $fileFactory;
        $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
        parent::__construct($context);
    }

    public function execute()
    {
        $name = date('m_d_Y_H_i_s');
        $filepath = 'export/custom' . $name . '.csv';
        $this->directory->create('export');
        /* Open file */
        $stream = $this->directory->openFile($filepath, 'w+');
        $stream->lock();
        $columns = $this->getColumnHeader();
        foreach ($columns as $column) {
            $header[] = $column;
        }
        /* Write Header */
        $stream->writeCsv($header);

        $products[] = array(1,'Test 1','test 1',100);
        $products[] = array(2,'Test 2','test 2',299);

        foreach ($products as $item) {
            $itemData = [];
            $itemData[] = $item[0];
            $itemData[] = $item[1];
            $itemData[] = $item[2];
            $itemData[] = $item[3];
            $stream->writeCsv($itemData);
        }

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

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

    /* Header Columns */
    public function getColumnHeader() {
        $headers = ['Id','Product name','SKU','Price'];
        return $headers;
    }
}

When you run controller action you can download CSV file using just Magento straight forward way.
Using the above way you can download CSV file using Magento 2 Way.