1

I have some data which is in Cyrilic and I wanted to export it in .csv file with Codeigniter. The problem is that the method csv_from_result() messes those characters.

$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";

$report_result =  $this->data_model->get_data_result();
$data['csv']    = $this->dbutil->csv_from_result($report_result, $delimiter, $newline);

And in my view I have:

<?php
header("Content-type: application/csv-tab-delimited-table; charset=utf-8" );
header("Content-Disposition: attachment; filename=csvdata.csv" );
header("Content-Transfer-Encoding: binary" );
  echo $csv; 
?> 

The output file, contains messed characters like these:

ТеÑÑ‚ пиÑмо","54","Референц
Glad To Help
  • 4,959
  • 2
  • 33
  • 52
  • Why are you using "application/csv-tab-delimited-table" instead of "application/csv" ? – user1190992 Aug 02 '12 at 15:12
  • I tried both ... I saw it somewhere, so I decided to give it a try, but it does not work – Glad To Help Aug 02 '12 at 15:14
  • Check the result that you get from the model and also try it without using the "Content-Transfer-Encoding" header. I think "Content-type: application/csv" and "Content-Disposition: attachment; filename=file.csv" are enough. – user1190992 Aug 02 '12 at 15:21
  • The result of the csv_from_result() is garbled, but if I for example use $result->result() the data is fine.. So it must be the csv_from_result() function – Glad To Help Aug 02 '12 at 15:32
  • What happens when you add this before you load the view $data['csv'] = mb_convert_encoding($data['csv'], 'UTF-8'); ? – user1190992 Aug 02 '12 at 15:48
  • Okay few more points you can try: use utf8_encode (http://us2.php.net/manual/en/function.utf8-encode.php) and check http://codeigniter.com/forums/viewthread/162559/ or maybe try a different approach to create the CSV file. – user1190992 Aug 02 '12 at 15:59
  • I guess I will have to manually generate the CSV string, instead of relying on Codeigniter's function csv_from_result() – Glad To Help Aug 02 '12 at 17:43

3 Answers3

2

csv_from_result from ci generates utf-8 without BOM, and you can find the difference here so all you need to do now is to convert "utf-8 without BOM" to "utf-8", and you can do it with the following code.

$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";

$report_result =  $this->data_model->get_data_result();
$CSV_data      = $this->dbutil->csv_from_result($report_result, $delimiter, $newline);
$CSV_data = chr(239) . chr(187) . chr(191) .$CSV_data;
$data['csv'] = $CSV_data;
Community
  • 1
  • 1
Prog Mania
  • 565
  • 6
  • 14
1

I ended up using PHPExcel for Codeigniter . That worked like a charm.

Glad To Help
  • 4,959
  • 2
  • 33
  • 52
0

Try this:

$this->load->dbutil();
$query = $this->db->query($sql);
$delimiter = ",";
$newline = "\r\n";

header ("Content-disposition: attachment; filename=csvoutput_" . time() . ".csv") ;
echo mb_convert_encoding($this->dbutil->csv_from_result($query, $delimiter, $newline), "ISO-8859-1", "UTF-8");
csotelo
  • 1,443
  • 2
  • 27
  • 42
  • Does not work, it converts the garbled characters to question marks (?) – Glad To Help Aug 07 '12 at 07:29
  • @GladToHelp change the from field to another encoding. My database was iso-8859-9, so I've switched the iso-8859-1 to iso-8859-9 worked fine. – Arda Oct 16 '14 at 09:30