2

I want to generate a csv file in var folder. I used these lines of code it's not working. Can someone help me to figure it out

public function execute()
{
    $filepath = 'export/customerlist.csv';
    $this->directory->create('export');
    $stream = $this->directory->openFile($filepath, 'w+');
    $stream->lock();

    $header = ['Id', 'Name', 'Email'];
    $stream->writeCsv($header);

    $collection = $this->customerFactory->create()->getCollection();
    foreach ($collection as $customer) {
        $data = [];
        $data[] = $customer->getId();
        $data[] = $customer->getName();
        $data[] = $customer->getEmail();
        $stream->writeCsv($data);
    }
}

}

1
  • give your path here $filepath = 'export/customerlist.csv';
    – Pramod
    Dec 15, 2021 at 5:17

1 Answer 1

2

just you need set var directory like this \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR try below code

namespace Vendor\Module\Controller\Adminhtml;

use Magento\Backend\App\Action;

class SampleCsv extends Action
{

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

    public function execute()
    {
        $name = date('m-d-Y-H-i-s');
        $filepath = 'export/sample-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 header name display in your CSV
        $columns = ['identifier', 'sku', 'special_price', 'deal_quantity', 'sort_order'];
        foreach ($columns as $column) {
            $header[] = $column; //column in Header array
        }
        $stream->writeCsv($header);

        $sampleData[] = array('aug2020', 'sku1', '100.00', '25', '1');
        $sampleData[] = array('sep2020', 'sku2', '2000.00', '32', '2');

        foreach ($sampleData as $data) {
            $itemData = []; //sample row data array
            $itemData[] = $data[0];
            $itemData[] = $data[1];
            $itemData[] = $data[2];
            $itemData[] = $data[3];
            $itemData[] = $data[4];
            $stream->writeCsv($itemData);
        }

        $content = [];
        $content['type'] = 'filename'; // must keep filename
        $content['value'] = $filepath;
        $content['rm'] = '1'; //remove csv from var folder
        $csvfilename = 'deals-offers-sample-' . $name . '.csv';

        return $this->_fileFactory->create($csvfilename, $content, \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);
    }
}
2
  • Is it possible to generate the same csv inside a code/vendorname/modulename/csv. I have tried this but its not working. $directory = $filesystem->getDirectoryWrite($directory::APP); $filepath = 'code/Vendorname/Modulename/File/Sample/refund.csv';
    – Mythili
    Dec 16, 2021 at 8:34
  • no, it's not possible. better create a folder in var directory whatever your module name same name. and save all CSV in that folder itself
    – Devidas
    Dec 16, 2021 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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