Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Export Selected Data into Excel Sheet or CSV file in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.60k
    Comment on it

    Simple code to fetch selected data from database table in excelsheet in cakephp:

    Step 1: To achieve this we will use csvHelper class. Now make a file named as CsvHelper.php in your app/View/Helper directory, then add the following code written below:

    <?php
    class CsvHelper extends AppHelper
    {
    var $delimiter = ',';
    var $enclosure = '"';
    var $filename = 'Export.csv';
    var $line = array();
    var $buffer;
    
    function CsvHelper()
    {
        $this->clear();
    }
    function clear()
    {
        $this->line = array();
        $this->buffer = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
    }
    
    function addField($value)
    {
        $this->line[] = $value;
    }
    
    function endRow()
    {
        $this->addRow($this->line);
        $this->line = array();
    }
    
    function addRow($row)
    {
        fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
    }
    
    function renderHeaders()
    {
        header('Content-Type: text/csv');
        header("Content-type:application/vnd.ms-excel");
        header("Content-disposition:attachment;filename=".$this->filename);
    }
    
    function setFilename($filename)
    {
        $this->filename = $filename;
        if (strtolower(substr($this->filename, -4)) != '.csv')
        {
            $this->filename .= '.csv';
        }
    }
    
    function render($outputHeaders = true, $to_encoding = null, $from_encoding ="auto")
    {
        if ($outputHeaders)
        {
            if (is_string($outputHeaders))
            {
                $this->setFilename($outputHeaders);
            }
            $this->renderHeaders();
        }
        rewind($this->buffer);
        $output = stream_get_contents($this->buffer);
    
        if ($to_encoding)
        {
            $output = mb_convert_encoding($output, $to_encoding, $from_encoding);
        }
        return $this->output($output);
    }
    }
    ?>

     

    Step 2: Now create a method export() in your controller class. export method is used to fetch selected data from your database table.

    function export()
    
    {
    
    $this->set('posts', $this->Post->find('all'));
    
    $this->layout = null;
    
    $this->autoLayout = false; Configure::write('debug','0');
    
    }

     

    Step 3: Next, In your controller(ex: PostController) add the following line of code:

    var $helpers = array('Html', 'Form','Csv');

     

    Step 4: Now we will create a file named export.ctp in app/View/ your controller to write resulting data in th excel sheet.

    <?php
    
    $line= $posts[0]['Post'];
    $this->CSV->addRow(array_keys($line));
     foreach ($posts as $post)
     {
       $line= $post['Post'];
       $this->CSV->addRow($line);
     }
     $filename='posts';
     echo  $this->CSV->render($filename);
    ?>

     

    Step 5: Last but not least create a link named export, on clicking the link you can get the data from your database table in excel sheet.

    <?php echo $this->Html->link('Export',array('controller'=>'posts','action'=>'export'), array('target'=>'_blank'));?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: