Create CSV Magento admin

This tutorial explains how to Create CSV Magento admin. Here I am explaining how to export CSV from Magento admin.


export-csv-magento-admin

File Structure

Create-CSV-Magento-admin

Also refer the other tutorials import magento 2 products.

Create CSV Magento admin

Please refer the following code.

System.xml

Add the following code in the system.xml file.

create-csv-magento-migrations

Block File

class Addpeople_Migrations_Block_Exportproducts extends Mage_Adminhtml_Block_System_Config_Form_Field
{

    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $this->setElement($element);
        $url = Mage::getModel('adminhtml/url')->getUrl("*/*/exportproducts");
        $html = $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setType('button')
            ->setClass('scalable')
            ->setLabel('Export CSV')
            ->setOnClick("setLocation('$url')")
            ->toHtml();

        return $html;

    }
}

Controller File

class Addpeople_Migrations_System_ConfigController   extends Mage_Adminhtml_Controller_Action
{
    /*
     * Export products in csv format
     *
     */
    public function exportproductsAction()
    {
        $data = array();
        $fileInfo = Mage::getModel('migrations/export_csv')->createCSV($data);
    }

}

Model Class - Create CSV Code

class Addpeople_Migrations_Model_Export_Csv extends Mage_Core_Model_Abstract
{
    public function createCSV( $data ) {

        $fileName = 'import_log'.date("Y-m-d").'.csv';

        $headers = ['Weight','Code','Product','SRP (GROSS)','SRP (NET)','30% Discount','Comment'];

        header('Content-Type: application/excel');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
        $file = fopen('php://output', 'w');
        fputcsv($file, $headers);

        foreach ($data as $row) {

            fputcsv($file, $row);

        }
        fclose($file);

    }

}

Config File

export-csv-magento-system-xml

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*