Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Export data into excel sheet using CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 1.98k
    Comment on it

    Hello friends, I am writing this blog which will help you to export data to excel sheet or you can say that it will write data into excel sheet with the help of 'php-export-data-class' library.

     

    Lets start by creating function name called export(). I am having sample example of Students here. You can create your own code. You need to download  php-export-data-class and place it in the component or inside the webroot folder.
    For now I have already placed this file inside the webroot folder. After this file 'php-export-data.class.php' has been uploaded to webroot folder, now you need to include this file using require_once in export() function.  Now define $exporter by typing the below code:

    $exporter = new ExportDataExcel('browser', 'students.xls');

    Here in above code we are writing 'browser' so that on load of the browser, export will begin as shown below:

     

     

    Now in order to start streaming data to web browser write the below code:

    $exporter->initialize();

     

    Now finally we need to add heading row to the excel file, you can do this by typing the code:

    $exporter->addRow(array("First Name", "Last Name", "Gender", "Year of Birth","Teacher Email ID","UID","Language","Level","Is Unique","Status"));

     

    So thats it, all done..  Now you can export the excel file. This is the complete code below:

    public function export(){
          $this->layout=false;
          require_once 'php-export-data.class.php';
          
          $students=$this->User->find('all',array('conditions'=>array('User.type'=>'student'),
                                        'fields'=>array(
                                            'User.first_name', 'User.last_name', 'User.gender', 'User.yob', 'User.username', 'User.language_id', 'User.level_id', 'User.is_unique', 'User.is_verified'),
                                        'contain'=>array(
                                        'Level.level_name','Language.language_name','Teacher.User1.email')));
          
          $exporter = new ExportDataExcel('browser', 'students.xls');
          $exporter->initialize(); // starts streaming data to web browser
          $exporter->addRow(array("First Name", "Last Name", "Gender", "Year of Birth","Teacher Email ID","UID","Language","Level","Is Unique","Status"));
          
          
          foreach ($students as $key => $data) {
              $exporter->addRow(array($data['User']['first_name'], $data['User']['last_name'], $data['User']['gender'], $data['User']['yob'],$data['Teacher']['User1']['email'],$data['User']['username'],$data['Language']['language_name'],$data['Level']['level_name'],$data['User']['is_unique'],$data['User']['is_verified']));
          }
           
          $exporter->finalize(); // writes the footer, flushes remaining data to browser.
          exit(); // all done
          
      }

     

    Thanks for reading the blog.

    Export data into excel sheet using CakePHP

 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: