简体   繁体   中英

PHP Export to CSV is putting all in 1 column

First a disclaimer, I am a noob here. I am trying to output my results into an excel file, however for some reason I can't seem to figure it, all the results are entering into the same column

Here is the function that I am using to convert it to the file:

function createCSVFile($filename,$headings,$data) {
    // Set Output Headers
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename={$filename}.csv");
    $fp = fopen('php://output', 'w');

    $head = array();
    foreach ($headings as $heading) {
      $head[] = $heading;
    }

    fputcsv($fp, $head);
    $rowData = array();
    // Put the data into the csv
    foreach ($data as $row) {

        foreach ($row as $item) {
            $rowData[] = $item;
        }
        fputcsv($fp, $rowData);
        unset($rowData);
    }
}

Here is some sample data and the structure of what I am passing:

$filename

$filename = "Staff-list";

$headings

$headings = array("Username","First Name","Last Name","Start Date", "End Date");

$data

$data = getStaffDetails();

Which returns:

array(6) {
[0]=>
  array(5) {
        [0]=>
    string(12) "James21"
        [1]=>
    string(3) "James"
        [2]=>
    string(9) "Michael"
        [3]=>
    string(10) "2016-06-01"
        [4]=>
    string(10) "0000-00-00"
  }
  [1]=>
  array(5) {
        [0]=>
    string(14) "thombommom"
        [1]=>
    string(5) "Thomas"
        [2]=>
    string(7) "Bomarius"
        [3]=>
    string(10) "2016-12-01"
        [4]=>
    string(10) "0000-00-00"
  }
}

And here is what it looks like in the ouput: 在此处输入图片说明

Update 1:

As per Marks solution below, I tried setting the deliminator in the PHP func to ; like so:

 fputcsv($fp, $head,';');

However, now the output just has semi colons instead of commas 在此处输入图片说明

One of the reasons I hate windows so much is that with every version, something is different making it harder for devs. Argh.... Anyway, what you need to do is override system setting ("list separator character") with: 'sep=;' .

The first thing you should pass to fputcsv is:

$seperator = "sep=;";
fputcsv($fp, $seperator, ';', ' ');

Then Make your data an array of arrays with the first array being your headings like so:

$data = array(array("One","Two", "Three"), array('1','2','3'),array('1','2','3'),array('1','2','3'));

Just because you said you're a noob I'll give ya the whole function:

function createCSVFile($filename,$data) {
    // Set Output Headers
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename={$filename}.csv");
    $fp = fopen('php://output', 'w');

    // This will override system setting ("list separator character") and Excel will open the file correctly.
    $seperator = "sep=;";
    fputcsv($fp, $seperator, ';', ' ');

    // Get the array initialised.
    $rowData = array();

    // put the data into the csv
    foreach ($data as $row) {

        foreach ($row as $item) {
            $rowData[] = $item;
        }
        fputcsv($fp, $rowData);
        unset($rowData);
    }
}

Keep me posted!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM