Export Data in CSV File

After some time we are back again with another post about Magento 2 and how to export data in a CSV file. Let's have a look together!

It is quite common that we need to export data in a CSV file so we can process it later usually by other systems or manually. In our example we will create a custom CLI command and by executing it we will export a CSV file with customer data under var/magevision_exports folder.

Firstly we declare our CLI command in the di.xml file like below.

With the following code we can achieve our goal. The CSV will include the customer firstname, customer lastname and customer email. You can easily extend it and add more data!

You can start the export by running bin/magento magevision:blog92:export-csv in

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge