[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.