How to Include Custom Column in Export To CSV in Magento 2

If you are a Magento 2 store admin, you must be quite acquainted with the backend grids. You also might frequently export the data of the grid for analysis, order management, stock management, etc.

Now, you might be unaware of the developer’s tricks!

I mean to say that there are certain columns in the admin grid that are there because of some operations under the column values from the database table.

For example, “mark 1” and “mark 2” columns are present in the database and the admin panel. Now “mark 3” is obtained by adding the values from the column mark 1 and mark 2 and displayed in the admin grid.

When you export to CSV from the admin panel, the mark 3 column won’t be exported along with the other columns sometimes as it is not present in the database table.

The below code can be used as the solution in such cases.

Also, if you want to export CSV with custom changes made, this method to include custom column in export to CSV in Magento 2 can be useful:

Steps to Include Custom Column in Export To CSV in Magento 2:

Create app/code/Vendor/Module/view/adminhtml/ui_component/component.xml

 

Create app/code/Vendor/Module/etc/di.xml

 

Create app/code/Meetanshi/CustomReportOrder/Model/Export/ConvertToCsv.php

Modify this file as per your requirement

Execute the below command:
php bin/magento cache:flush

That’s it.

You can learn the method to add a custom column in order grid in Magento 2 in the first place. Or you can just install Magento 2 Custom Order Grid extension that allows adding 50+ custom columns in Magento 2. Not only that, but it also offers the facility to track the order information and improve the order processing and management system in Magento 2 store.

Any doubts about the topic or the solution can be mentioned in the Comments section below and I’d be helping you out asap.

Please share the solution with the Magento community via social media.

Thank you.

4.7
(based on 7 Reviews)
How to Include Custom Column in Export To CSV in Magento 2Author Magento Badge

Sanjay Jethva

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

13 Comments

  • Jorge Eduardo Olaya

    Thanks for sharing your work, but how do I instantiate the class. Let me explain: how do I place the export button?

  • Jorge Eduardo Olaya

    Thanks for sharing your knowledge. But it didn’t work for me. I want to place a button in the products view to export. I always get an error. Try if code and nothing.

  • Nitish Kesarwani

    I have add two columns in sales_order_grid.xml in ui component , column is showed in csv file but data is missing in column. I used this command
    if ($component->getName() == ‘sales_order_grid’)
    {
    foreach ($items as $item)
    {
    $this->metadataProvider->convertDate($item, $component->getName());
    $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
    }
    }
    else
    {
    foreach ($items as $item) {
    $this->metadataProvider->convertDate($item, $component->getName());
    $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
    }
    }

    • Sanjay Jethva

      Hello Nitish,
      Regarding your query, it is a custom requirement and you need to put extra effort in order to fulfill your requirement.
      Add a log and check the log step by step
      Thank You

  • bhavin

    Hi
    not Working ..
    No any error..
    Please tell me what is the issue?
    Thank you

    • Sanjay Jethva

      Hello Bhavin,
      It is working from our end. Can you please check that instead of “customReportOrder_post_listing” you put the right name?
      I hope it helps.

  • Ludoivco

    Hi,
    in Magento 2.3.5 the button does not appear

    Regards

  • train

    I just want to export columns from catagory/products but can not get it to work.

    • Sanjay Jethva

      Hello Train,

      The default 2 does not allow to export from the product grid to generate a CSV file. This blog is for the grids having an export to CSV option in which you can add the custom column.

      Thank you.

  • Magento Ecommerce

    Great content , really informative and useful , appreciate you enlightening us with this!

Leave a Reply

Your email address will not be published.