Magento 1.9 – Issue in Exporting CSV File from Custom Module

csvexportmagento-1.9

I have a custom module in admin panel,when I click at "Export" button following function is called. CSV is being exported and downloaded correctly, in CSV one column is having id's instead of value of field.So I just want to changes to the value for that column, But I can't find where data is being dumped into csv file.

class Abc_AddressManager_Adminhtml_StoresController extends
Mage_Adminhtml_Controller_Action {
 public function exportCsvAction() {
    $fileName = 'stores.csv';
    $content = $this->getLayout()->createBlock('abc_addressmanager/adminhtml_stores_csv')
            ->getCsvFile();

    $this->_prepareDownloadResponse($fileName, $content);
 }
}

And

class Abc_AddressManager_Block_Adminhtml_Stores_Csv extends Mage_Adminhtml_Block_Widget_Grid
{

protected function _prepareCollection() {
    $customer_id = $this->getRequest()->getParam('id');

    $collection = Mage::getResourceModel('customer/address_collection');
    $collection->addAttributeToSelect('*');
    $select = $collection->getSelect();

    $select->joinLeft(
            array('customer' => $collection->getTable('customer/entity')), 'customer.entity_id=e.parent_id', array('email' => 'email')
    );

    $d = $collection->getSelect()->__toString();
    $this->setCollection($collection);

    return parent::_prepareCollection();
}
protected function _prepareColumns() {

    $addressModel = Mage::getModel('customer/address');
    $addressForm = Mage::getModel('customer/form');
    $addressForm->setFormCode('adminhtml_customer_address')
            ->setEntity($addressModel)
            ->initDefaultValues();
    $attributes = $addressForm->getAttributes();

    $this->addColumn('email', array(
        'header' => 'email',
        'type' => 'text',
        'index' => 'email',
        'filter' => false,
        'sortable' => false,
    ));
$this->addColumn('entity_id', array(
            'header' => 'address_id',
            'type' => 'text',
            'index' => 'entity_id',
            'filter' => false,
            'sortable' => false,
        ));
    foreach ($attributes as $attribute) {

        $code = $attribute->getData('attribute_code');

        $this->addColumn($code, array(
            'header' => $code,
            'type' => 'text',
            'index' => $code,
            'filter' => false,
            'sortable' => false,
        ));
    } // end 

    return $this;
}

My field Column is "Ownership", that is a customer attribute, type drop down,its attribute_id =554 in eav_attribute table, and option id's are =438 to 443 in eav_attribute_option, and values for option is stored in eav_attribute_option_value,in csv file, column name is Ownership, but its has option id's(438 to 443) in it, instead of their value. My grid look like this.As you can see, Ownership has value(CO), in csv it here I am getting '439' option_id for "CO"
Admin grip

Best Answer

You should override _prepareColumns function inside your custom block

Class abc_Addressmanager_Block_Adminhtml_Stores_Csv extends Mage_Adminhtml_Block_Report_Grid_Abstract {

     protected function _prepareCollection()
     { 
          //please make sure to set collection here and having all columns you want to load in the query
     }

     protected function _prepareColumns()
     {
        //for debuging
        $collection = $this->getCollection();
        //printing the query using this command (if $collection object is set)
        echo   $collection->getSelect();
        //making sure the fields you want to show up is in the query output
        //you can add or remove columns here
     }    

}
Related Topic