Magento Generate CSV Export for Custom Admin grid

By | March 15, 2014

Ever Seems we require to Export the CSV or XMl for the custom grid for admin we need to follow small piece of code and you are ready to get the result

In your custom grid class you need to call method “addExportType” from “Mage_Adminhtml_Block_Widget_Grid” class
Code of protected function _prepareColumns() in your Grid class:

Add the below code under _prepareColumns.

$this->addExportType('*/*/exportCsv',
         Mage::helper('your_helper')->__('CSV'));
return parent::_prepareColumns();

This will generate the Export button and once that done go to controllers file and the action to call the CSV or XMl

public function exportCsvAction()
{
    $fileName   = 'yourfilename.csv';
    $content    = $this->getLayout()->createBlock('modulename/adminhtml_modelname_grid')
    ->getCsvFile();
    $this->_prepareDownloadResponse($fileName, $content);
}
protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
{
$this->_prepareDownloadResponse($fileName, $content, $contentType);
}

and once that done CSV will be generate.”Mage_Adminhtml_Block_Widget_Grid” class have other methods also for generating Excel files.

Hope this will help you

4 thoughts on “Magento Generate CSV Export for Custom Admin grid

Leave a Reply to fabien Cancel reply