0
public function ExportCSV(){
    $this->load->dbutil();
    $this->load->helper('file');
    $this->load->helper('download');        
    $delimiter = ",";
    $newline = "\r\n";
    $slash = "∕";
    $filename = "testnaja.csv";
    $query = "select * from student where room_id = 011";
    $result = $this->db->query($query);
    $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);               
    force_download($filename, $data);
}    

I want to generate excel file exporting data from my table to it.
The output in .xls and .csv

In csv output is:

enter image description here

Please help. Thanks.

3 Answers 3

4

Change your force_download from

force_download($filename, $data);

To this:

force_download($filename, "\xEF\xBB\xBF" . $data);

which makes the output UTF-8 + BOM so it's recognized by MS Excel

Sign up to request clarification or add additional context in comments.

Comments

2

I don't know if I understood you correctly, but if your problem is that you'd like Excel to recognize your CSV file as being UTF-8 encoded, you'll have to add a UTF-8 BOM to your file. Just prepend your CSV output with

chr(239) . chr(187) . chr(191) // xEF xBB xBF

For example:

$data = chr(239) . chr(187) . chr(191) . $this->dbutil->csv_from_result($result, $delimiter, $newline);

If your problem is about forcing a download, perhaps https://stackoverflow.com/a/33593363/11354 is going to help.

Comments

0

To setup codeigniter to download CSV format, use the following code.

function _push_file($path, $name)
{
  // make sure it's a file before doing anything!
  if(is_file($path))
  {
    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    $this->load->helper('file');

    $mime = get_mime_by_extension($path);

    // Build the headers to push out the file properly.
    header('Pragma: public');     // required
    header('Expires: 0');         // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
    header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($path)); // provide file size
    header('Connection: close');
    readfile($path); // push it out
    exit();
}

Duplicate from : Codeigniter Force download files

and also from : Download csv from codeigniter mysql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.