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);
 }
}

}

asked Dec 14, 2021 at 16:30
1
  • give your path here $filepath = 'export/customerlist.csv'; Commented 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);
 }
}
answered Dec 15, 2021 at 6:00
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'; Commented 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 Commented Dec 16, 2021 at 9:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.