Welcome Guest, Not a member yet? Register   Sign In
Issue with comma on csv export
#1

[eluser]alberto24[/eluser]
Hi - happy new year to all CI people!

I am running into a big issue with my app - I have a function that creates a csv file of data in the database. Sometimes the data in the database field is:
Professor, Economics

When the comma is in the field of data, and then the file is exported, the comma screws up the file and pushes Economics out to the next field.

Any ideas on how to fix this? I tried using a pipe as a delimiter but then Excel doesn't understand how to handle the file on simple open - I am dealing with mostly very basic computer users so I'd prefer to stick to the comma.

Here's a snippit of the code:

Code:
foreach ($result->result() as $vcard)
        {
            $data .= $vcard->f_name . ",";
            $data .= $vcard->l_name . ",";
    
        }
        force_download($name, $data);

Thank you in advance
#2

[eluser]alberto24[/eluser]
ok - i had a friend help me add " around the data and it seems to work ok
thanks
#3

[eluser]Unknown[/eluser]
Hi,
I have the same issue with my date field. The date will be in the following format Tue, 18 Aug 09 07:13:22 +0000. Because of the comma in between the day & date, Excel file considers this as a delimiter and splits it up. I tried to add a single quotes around the data, as suggested in the previous thread. But it failed miserably.

Your help will be appreciated. Please help me.

I have pasted the code for your reference below,

Code:
<?php
            $date           = "Tue, 18 Aug 09 07:13:22 +0000";
            $result         = array("08/12/2009 23:59:00", "08/12/2009 23:59:00", "08/12/2009 23:59:00", "08/12/2009 23:59:00");
            $valueString    = "Report generated on: ".$date."\r\n\r\n";
            if($result != '')
            {
                foreach($result as $data)
                {
                    $valueString     .= $data."\r\n";
                    
                }
            }
            $output        = $valueString;
            $ctype         = "application/vnd.ms-excel";
            $fileName      = "RawData-".date("Y-m-d")."-".time().".csv";
            header("Content-type: application/vnd.ms-excel");
            header("Pragma: public"); // required
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false); // required for certain browsers
            header("Content-Type: $ctype");
            header("Content-Disposition: attachment; filename=\"".basename($fileName)."\";" );
            header("Content-Transfer-Encoding: binary");
            print $output;
            exit();
            
?>




Theme © iAndrew 2016 - Forum software by © MyBB