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
... View more