cancel
Showing results for 
Search instead for 
Did you mean: 

Write CSV file Magento 2.2

SOLVED

Write CSV file Magento 2.2

Hi Everyone,

 

i'm trying to write a custom module for Magento 2.2. It have to write a .csv file. I read on different sites somebody that use class in \Magento\Framework\App\Response\Http\FileFactory but if I open \Magento\Framework\App\Response I don't have \Http\FileFactory. 

Someone can help me? Maybe it a stupid problem but I'm new in development of Magento custom module.

 

Thank you guys

8 REPLIES

Re: Write CSV file Magento 2.2

Hi @testother,

 

Maybe you'll find useful to check how the Export works on Magento to get an idea of which classes are being used.

I.e.: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/ImportExport/Controller/Adminh...

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: Write CSV file Magento 2.2

You can write csv file using below way,

 

<?php

public function __construct(
    \Magento\Framework\File\Csv $csvProcessor,
    \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
    \Magento\Framework\Filesystem $filesystem
    )
{
    $this->filesystem = $filesystem;  
    $this->directoryList = $directoryList;
    $this->csvProcessor = $csvProcessor;
}

function writeToCsv(){
	$fileDirectoryPath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);

	if(!is_dir($fileDirectoryPath))
	    mkdir($fileDirectoryPath, 0777, true);
	$fileName = 'export.csv';
	$filePath =  $fileDirectoryPath . '/' . $fileName;

	$data = [];
      /* pass data array to write in csv file */
	$data[] = ['orderid' => '100001'];
	$this->csvProcessor
	    ->setEnclosure('"')
	    ->setDelimiter(',')
	    ->saveData($filePath, $data);

	return true;
}

 You can check your generated CSV file inside var folder

If Issue Solved, Click Kudos/Accept As solutions.

Re: Write CSV file Magento 2.2

Hi @Rakesh Jesadiya,

 

thanks for the code, but the problem is that I don't have these path:  \Magento\Framework\File\Csv ; \Magento\Framework\App\Filesystem\DirectoryList ;

 \Magento\Framework\Filesystem

(I have \Magento\Framework and \Magento\Framework\App of course)

Re: Write CSV file Magento 2.2

Hello @Damian Culotta,

thanks for the answer. But I can't find these necessary classes in my Magento\Framework 

Re: Write CSV file Magento 2.2

Have you installed Magento 2 using the command line or using a downloaded zip file?

If Issue Solved, Click Kudos/Accept As solutions.

Re: Write CSV file Magento 2.2

@Rakesh Jesadiya,

 

no, I used an installation tool provided by the server host.

 

UPDATE: I have just installed again Magento using .zip file and I still can't find those classes in \Magento\Framework

Re: Write CSV file Magento 2.2

You can find your magento framework module under lib folder in root.

 

your path will be,

lib/internal/Magento/Framework/File/Csv.php
If Issue Solved, Click Kudos/Accept As solutions.

Re: Write CSV file Magento 2.2

@Rakesh Jesadiya,

 

you're right. I have checked on my Magento Installation and there isn't the path 

lib/internal/Magento/

But, checking on another Magento Installation (local) I founf it. Maybe there were some problem with installation. Thank you so much