Excel CSV helper for CodeIgniter

array to csv

$array = array(
	array('Last Name', 'First Name', 'Gender'),
	array('Furtado', 'Nelly', 'female'),
	array('Twain', 'Shania', 'female'),
	array('Farmer', 'Mylene', 'female')
);
 
$this->load->helper('csv');
echo array_to_csv($array);
"Last Name","First Name",Gender
Furtado,Nelly,female
Twain,Shania,female
Farmer,Mylene,female

Direct download

array_to_csv($array, 'toto.csv')

query to csv

$this->load->database();
$query = $this->db->get('my_table');
 
$this->load->helper('csv');
echo query_to_csv($query);

Direct download

query_to_csv($query, TRUE, 'toto.csv')
 

Feedback

Hey can you please help me get the titles for array to csv. I wanna use the array keys as the title (like they do in the query to csv). Can you please tell me how to do it with your helper? mmarcus44@gmail.com
Michael
February 25, 2009
#1
This was a big help to get me going, thanks! In the future I may add in the CI Download Helper usage if anything. @Michael and for anyone else: You can use array_keys and array_values if you need to pull out those pieces of information as a "regular" array with numeric indexes.
Joshua
May 4, 2009
#2
Searching something like this. Thanks Joshua for this great help. :-D
Russel
July 28, 2009
#3
Thanks for this helper. It saved me a lot of work and it's very robust and yet simple to use. I truly appreciate it. Keep up the good work mate!
Rick C.
September 21, 2009
#4
//function that convert an object to an array was useful for mi function objectToArray( $object ) { if( !is_object( $object ) && !is_array( $object ) ) { return $object; } if( is_object( $object ) ) { $object = get_object_vars( $object ); } return array_map( 'objectToArray', $object ); } //hope it helps
uoL
November 11, 2009
#5
Hi can i use this helper and call directly the model function and export it to csv? Thanks please send me to coder5@ymail.com
coder5
January 12, 2010
#6
Beautifully simple, and still works!! Thank you
RJ
January 17, 2010
#7
thank you, very simple and very very useful..
Az
February 7, 2010
#8
Thaks for your helper, it's very simple to use and very useful.

Bur i wonder if i can export the data in a xls format directly. Currently (i implemented this into a client's application), my client has to export the data, open excel, choose import data from file, and select comma separated value.

Anyway, amazing simple helper.

Thanks again
Jesus
February 23, 2010
#9
great

I use this

amazing


mahrizal
April 29, 2010
#10
great

I use this

amazing
Crazy Huy
May 20, 2010
#11
Many thanks! Very handy, saves quite a bit of time! :)
Ethan
June 10, 2010
#12
Thanks for this helper.But I have some problem when I convert to csv file my session have been not exist I want my session to be alive. How I can do?.
KayZ
July 6, 2010
#13
compress output = FALSE

otherwise you will loose hours trying to see where the problem starts
one2one
August 2, 2010
#14
This method exhausts an unusually high amount of RAM and does not work for me for anything over 1,500 lines or so. Any ideas to fix it?
NexusRex
August 8, 2010
#15
Here's how I'm using it:

// Export all locations for the specified account
// Will provide a CSV file to download
function export_all($id) {
$this->config->set_item('compress_output', FALSE);
$this->load->helper('csv');

// Get all locations
$query = $this->locations_m->get_all($id);
$this->db->select('*');
$this->db->from('locations');
$this->db->where('account_id', $id);
#$this->db->limit(1000);

$query = $this->db->get();
#$array = $query->result_array();

query_to_csv($query, TRUE, 'export_all.csv');
}
NexusRex
August 8, 2010
#16
I receive an error when I use the array_to_csv function

This webpage is not found.

No webpage was found for the web address:
12Bo
December 7, 2010
#17
It doesn't seem to like the stdClass Object being used as the array. Any thoughts on resolving this issue?

12Bo
December 7, 2010
#18
Works like a charm! Nice work and thanks for sharing with the community.
Roger
December 30, 2010
#19
I am trying to exports csv/xls using this helper.

That works fine with every browsers except IE. Every time i export csv files it resets IE session.

Any help will appreciable.
Russel
January 16, 2011
#20
Very nice :)
Sunbni
January 25, 2011
#21
Thanks man!!!.

Very useful.
Taytus.com
January 31, 2011
#22
$%$%#$%#%
yes ,it can work,but when it export a chinese record to a .csv file, they are the wrong characters like that '#%#%%@#'.Why? What can i do? waiting for Online!!!!
Eric
February 15, 2011
#23
thanks..
successfull
neoryzer
February 15, 2011
#24
Hi bit of a glitch for me.

my query is as follows

"
$query = $this->db->where(array('Dial_Up' => '0', 'Language' => 'English', 'Membership_Status' => 'Active'));
$query = $this->db->like('Email', '@');
$query = $this->db->get('Membership');
return $query->result();
"

I would like the csv file to contain only two fields, ID and Email.

How would I go about that?
Jon York
March 24, 2011
#25
Nevermind, Got it, just had to use select commands!
jon york
March 24, 2011
#26
HOW CAN I FIX THE COLUMN SIZE OF CSV FILE ...?
TANIM
April 18, 2011
#27
there's an error A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/csv/system/application/models/csv_model.php:27)

Filename: helpers/csv_helper.php

Line Number: 27

i dont know how to solve it..please help me guys!! thanks a lot in advance
kristine
May 2, 2011
#28
Same issue here with the headers... I disabled the profiler, and I don't have anything else being sent to the browser that I know of. Help?
dsheetz
May 16, 2011
#29
I used .htaccess to set output_buffering to on, and it worked.

Only problem I'm having now is that there's a bunch of carriage returns at the top of the file... anyone else experiencing this?
dsheetz
May 16, 2011
#30
Very userful. Thanx
Pablo
June 9, 2011
#31
Had written my whole code and then stumbled on this one. Ended up using this one !!!! nice work.
Shikhar
June 17, 2011
#32
hi,, can i set the delimiter with this helper??
ibnu
July 26, 2011
#33
Thanks ... Its very usefull ..
Linzay Mughal
August 11, 2011
#34
when send the csv to file too send the html file
Roberto Estupiñán Pérez
October 18, 2011
#35
Thanx...its very usefull for me..

but how to set delimiter with ";" ?
bradercep
January 28, 2012
#36