Welcome Guest, Not a member yet? Register   Sign In
converted to_excel plugin to helper to work in CI 2.0
#23

[eluser]shivPrasad[/eluser]
HI all, I am newbie in CI.
using to_excel library I'm able to export the file; but the headers are not exported in excel.

Here is To_excel library copied from net:
Code:
/*
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

|--------------------------------------------------------------------------
| Create Excel file - Library
|--------------------------------------------------------------------------
| Created:  24/04/2013
|
|       Excel library for Code Igniter applications
|       Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
|       https://github.com/EllisLab/CodeIgniter/wiki/Excel-Plugin
|
|       Updated by: ccontreras: 19 May 2011 11:59 AM
|      
|       Updated for Codeigniter 2.1.3  and converted to library file
|       by Timothy Head: 11/06/2013
|
*/
class To_excel {
    
    function create_excel($query, $filename='exceloutput')
    {
        $ci =& get_instance();
        $ci->load->helper('download');
    
        $headers = ''; // just creating the var for field headers to append to below
        $data = ''; // just creating the var for field data to append to below        
  
  
        $fields = $query->list_fields();
  //$fields = $query->field_data();
        if ($query->num_rows() == 0) {
            echo '<p>The table appears to have no data.</p>';
        } else {
            foreach ($fields as $field) {
               //$headers .= $field->name. "\t";
      $headers .= $field. "\t";
            }
    
            foreach ($query->result() as $row) {
                $line = '';
                /*foreach($row as $value) {                                            
                    if (( ! isset($value)) OR ($value == "")) {
                        $value = "\t";
                    } else {
                        $value = str_replace('"', '""', $value);
                        $value = '"' . $value . '"' . "\t";
                    }
                    $line .= $value;
                }
                $data .= trim($line)."\n";
            }*/
   foreach($row as $value) {                                            
     if ((!isset($value)) OR ($value == "")) {
    $value = "\t";
     } else {
    $value = str_replace("\n", " ", $value); // for stupid line breaks that mess up the spreadsheet
    $value = str_replace('"', '""', $value);
    $value = '"' . $value . '"' . "\t";
     }
     $line .= $value;
   }
   $data .= trim($line)."\n";
    }          
            $data = str_replace("\r","",$data);  
            force_download($filename . ".xls", $headers . "\n" . $data);
   /*header("Content-type: application/x-msdownload");
       header("Content-Disposition: attachment; filename=$filename.xls");
      echo "$headers\n$data";*/
        }
    }
}
/* End of file */
/* Location: ./application/libraries/To_excel.php */
Function in the controller :

Code:
public function export($todate,$frmdt,$crite)
{
  //load library
  $this->load->library('To_excel');  
  $this->load->model('admin_model');
  //pass processed query to generate excel file.
  $data = $this->admin_model->reportsPaid($todate,$frmdt,$crite,1);
  // Create Excel file
  
  $this->to_excel->create_excel($data, 'PaidReport'); // filename is optional, without it, the plugin will default to 'exceloutput'
}//

Please guide me on this.


Messages In This Thread
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:00 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:02 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:32 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:46 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:47 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 08:53 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-18-2011, 09:15 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-22-2011, 11:24 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-22-2011, 11:28 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 04-22-2011, 11:30 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 08:01 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 08:03 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 09:38 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 10:22 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 01:09 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 01:39 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 02:01 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 02:02 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-08-2011, 02:05 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-10-2011, 01:02 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 06-10-2011, 01:10 PM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 03-28-2012, 09:23 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 12-27-2013, 03:45 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 12-29-2013, 11:44 AM
converted to_excel plugin to helper to work in CI 2.0 - by El Forum - 12-30-2013, 09:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB