How to generate your own reports in Magento

How to generate your own reports in Magento

Coolest thing in Magento’s is generating your own reports.
The quickest way to create Magento’s reports is to generate them through the grids.

First you will need to create collection of data which you want to display and save.
Then you have to add those fields in your grid, the rest is piece of cake.

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

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

– this code generates export button in your grid and calls “exportCsvAction” in controller of your module, put it anywhere above “return parent::_prepareColumns();

Your button will appear in grid:

Now, all you need to do is create method in called “exportCsvAction” in your controller class.
Code looks like this:

	public function exportCsvAction() {
		$fileName = 'your_custom_file.csv';
 
		$content = $this->getLayout ()
			->createBlock('your_custom_csv_grid')
			->getCsvFile();
 
		$this->_prepareDownloadResponse($fileName, $content);
 
	}

Note: You can study “Mage_Adminhtml_Block_Widget_Grid” class and find out that there are also methods for generating Excel files.

Enjoy coding.

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 35

How To Connect Google Analytics 4 To Magento 2

3 best open-source eCommerce platforms in 2021 Zrinka Antolovic
Zrinka Antolovic, | 7

3 best open-source eCommerce platforms in 2021

Delete test orders in Magento Dejan Radic
Dejan Radic, | 195

Delete test orders in Magento

6 comments

  1. Hi , i am trying to generate reports of transactions page and have applied the steps you have mentioned above but on clicking of export button i am getting this error :

    “You cannot define a correlation name ‘sop’ more than once”

    Please help

  2. Nice Article !

    I’m getting this fatal error while clicking export button
    Call to a member function getCsvFile()

    I used below code in controller.

    $content = $this->getLayout()->createBlock(‘importedit/adminhtml__grid’)->getCsvFile();

    Where i’m wrong !

  3. Thanks for the article. I’m a bit confused by the goal of the article. The title made me think this is about creating custom reports in Magento, but reading the article seems like it’s about adding an Export To Excel button to an already created custom report. Am I missing something?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.