How to Create a CSV File to Download in Magento 2

How to Create a CSV File to Download in Magento 2

A simple file format, CSV files are of great help to store tabular data. Magento 2 developers use it a lot for various purposes. Keeping in mind the popularity of CSV file format and its uses in the development process, I came up with a method to create a CSV file to download in Magento 2.

One can use this method to create a CSV file for product attributes data, customers’ data, etc.

Earlier, I had given the solution to import CSV in Magento configuration and now you may bookmark the method for Magento 2.

Steps to Create a CSV File to Download in Magento 2:

That’s it.

Feel free to ask your doubts in the Comments section below and I’d help you out.

I’d be grateful if you could share the above solution on social media with fellow developers.


(based on 4 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. When he is not engrossed with anything related to Magento, he loves to play cricket.

4 comments On How to Create a CSV File to Download in Magento 2

  • Thanks, super helpful. But I think something is missing in the Custom Button code at the beginning.

  • this looks really good. How can I create a button in sales_order_invoice_grid.xml to call this controller?

    • Hi Frank, to create a button, follow the below steps:

      create file at app/code/Vendor/Module/view/adminhtml/ui_component/sales_order_invoice_grid.xml

      < xmlversion="1.0" encoding="UTF-8">
      < listing xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
      < settings>
      < buttons>
      < button name="CustomButton" class="Vendor\Module\Block\Adminhtml\Sales\CustomButton"/>
      < /buttons>
      < /settings>
      < /listing>


      authorization = $authorization;
      $this->context = $context;
      * @return array
      public function getButtonData()
      if (!$this->authorization->isAllowed(‘Magento_Cms::save’)) {
      return [];
      return [
      ‘label’ => __(‘Custom Button’),
      ‘on_click’ => sprintf(“location.href = ‘%s’;”, $this->getBackUrl()),
      ‘class’ => ‘primary’,
      ‘sort_order’ => 10
      * Get URL for back (reset) button
      * @return string
      public function getBackUrl()
      return $this->context->getUrlBuilder()->getUrl(‘[router]/[controller]/[action]’, []); // here add csv controller action

Leave a Reply