Magento 2 Import and Export.
18 August 2019
It’s Important to learn how to add Magento 2 import export functionality to your module in Magento 2.
All the classes you need to Import or Export your data can be found in
vendor/magento/module-import-export
library.
The purpose of this article is to explain the functionality of the Magento 2 Import Export module. I’ll assume you know how to create the module for Magento already.
To simplify, Let’s assume your database is a list of Suburbs Data with the following Table structure.
- suburb_id
- suburb_name
- suburb_code
- suburb_state
Importing data.
To import data, you can extend the
Magento\ImportExport\Model\Import\Entity\AbstractEntity
Class
First create import.xml file in your etc folder and include your import entity details
In your Forgeonline\Dataimportexport\Model\Import\Suburbs
you need to create three abstract functions
public function _importData()
public function validateRow()
public function getEntityTypeCode()
In your validateRow()
you can use $this->getBehavior($rowData)
to validate against following
\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND
\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND
\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND
Please refer the file at Github
Exporting data.
Since our database table is simple, we can export data in admin suburb gird layout list. we can include the massAction export function to the Suburbs admin grid Actions list.
Lets build the mass export action for our Suburbs:
First you need to assign massExport action to in your admin view ui_component list.
I guess you’ve used the Magento UI Components library for you admin grid layouts.By using UI Component libraries not only can you build basic Magento functions much faster, but you will gain access to other cool functions like massActions, Filters etc.
Finally, you need to create your massExport.php
action inside the controller.
In your massExport file you need to load massAction libraries
Magento\Ui\Component\MassAction\Filter
Magento\Ui\Model\Export\ConvertToCsv
Magento\Framework\Filesystem
Magento\Framework\App\Response\Http\FileFactory
In your public function execute()
you can write your logic to create csv for download.
If you need more information, take a look at Magento\Ui\Component\MassAction\Filter
class.
Please refer the example controller file in Github
Forge Online Magento Website Development