Welcome Guest, Not a member yet? Register   Sign In
Download in excel format
#1

[eluser]Tamilmani[/eluser]
Hi

Anybody tell to me .. How i download the datas(retrieve from DB) in excel format using CI and Database.

Please Urgent for me ...

Give me the idea or example link
#2

[eluser]Thorpe Obazee[/eluser]
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
* Excel library for Code Igniter applications
* Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
*/

function to_excel($query, $filename='exceloutput')
{
    $headers = ''; // just creating the var for field headers to append to below
    $data = ''; // just creating the var for field data to append to below
    
    $obj =& get_instance();
    
    $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";
        }
    
        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)."\r\n";
        }
        
        $data = str_replace("\r","",$data);
                    
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=$filename.xls");
        echo "$headers\n$data";  
    }
}
?&gt;

Plugin from Bamboo Invoice
#3

[eluser]Tamilmani[/eluser]
Thanks dude i ill try that
#4

[eluser]Thorpe Obazee[/eluser]
It should probably work since I've used it a couple of times.
#5

[eluser]Tamilmani[/eluser]
Hey dude i am installed the bamboo invoice but i didn't seen any functionality for excel download . Here avilable for generate pdf
#6

[eluser]Thorpe Obazee[/eluser]
Errmm... did the plugin wok for you or not?
#7

[eluser]Tamilmani[/eluser]
not work
#8

[eluser]Thorpe Obazee[/eluser]
show your code.

I might be out in a minute.
#9

[eluser]Tamilmani[/eluser]
Controller function
----------------------
function dump(){
$this->load->plugin('to_excel');
$this->db->use_table('users');
$this->db->select('user_name', 'first_name');
// run joins, order by, where, or anything else here
$query = $this->db->get();
to_excel($query);
}


This is my plugin file

&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function to_excel($query, $filename='exceloutput')
{
$headers = ''; // just creating the var for field headers to append to below
$data = ''; // just creating the var for field data to append to below

$obj =& get_instance();

$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";
}

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";
}

$data = str_replace("\r","",$data);

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=$filename.xls");
echo "$headers\n$data";
}
}
?&gt;
#10

[eluser]Thorpe Obazee[/eluser]
I believe you need an actual query and not the result set in to_excel();




Theme © iAndrew 2016 - Forum software by © MyBB