2

I am generating a CSV downloadable file in PHP. I have a problem that when I open file in MS Excell or Ubuntu Libre Office the formate is not like what is expected.

I want this output but I can not force to start from new line.

Name           Slope   Length   Size(Inches)      Max Length         Location                                       
Name1            5      150       12"                500            location1   
Name2            8      350       12"                400            location 2  
Name3           16      326       12"                400            location3 

This is my PHP code

        $csv_data[] = $_POST['name'][$i];
        $csv_data[] = $_POST['slope'][$i];
        $csv_data[] = $_POST['length'][$i];
        $csv_data[] = $size;
        $csv_data[] = $max_length;
        $csv_data[] = $_POST['location'][$i].PHP_EOLE;


    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=file.csv');

    $output = fopen('php://output', 'w');
    fputcsv($output, array("Name", "Slope", "Length", "Size", "Max Length", "Location"));
    fputcsv($output, $csv_data);

But this code output like this:

Name    Slope   Length  Size    Max Slope Length    Location                                                
Name1   5   150 12" 500 location1   Name2   8   350 12" 400 location 2  Name3   16  326 12" 400 location3
tshepang
  • 12,111
  • 21
  • 91
  • 136
Noor
  • 1,351
  • 8
  • 27
  • Look at the file in an editor that can read Unix or Windows newlines correctly – Mark Baker Jan 11 '13 at 07:50
  • 1
    I read this output in MS WordPad and the output was nearly as expected but I need it also to be readable in MS Excell. – Noor Jan 11 '13 at 07:54
  • Try reading the output in notepad.exe **not wordpad**. Please post your full code. – Shiplu Mokaddim Jan 11 '13 at 08:05
  • possible duplicate of [How can I change the line endings used by fputcsv?](http://stackoverflow.com/questions/12722894/how-can-i-change-the-line-endings-used-by-fputcsv) – Ilmari Karonen Jan 11 '13 at 08:26
  • MS Excel doesn't care whether it's \n\r, \r\n or simply \n, it should work with them all... WordPad !== MSExcel – Mark Baker Jan 11 '13 at 08:56
  • @shiplu.mokadd.im: Now I paste here full code, and the output is also shown on one line in Notepad – Noor Jan 11 '13 at 11:56

3 Answers3

1

Do the following:

$csv_headers = array(array("Name", "Slope", "Length", "Size", "Max Length", "Location"));
$csv_full_data = array_merge($csv_headers, $csv_data);
var_dump($csv_full_data);
fputcsv($output, $csv_full_data);

And tell us the output.

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
  • 1
    I try doing this your way and this is the output, its still on one line Array Name1 5 150 12" 500 location1 Name2 8 350 12" 400 location12 Name3 16 326 12" 400 location3 Name4 36 127 12" 400 location4 – Noor Jan 11 '13 at 11:51
  • 1
    Thanks it did not work for me, but it give me an idea, I solve the problem. – Noor Jan 11 '13 at 12:13
1

What I did is I make the $csv_data array two dimensional instead of one and then I use foreach loop. Its a bit slow but working for me.

Here is the updated code

    $csv_data[$i][] = $_POST['name'][$i];
    $csv_data[$i][] = $_POST['slope'][$i];
    $csv_data[$i][] = $_POST['length'][$i];
    $csv_data[$i][] = $size;
    $csv_data[$i][] = $max_length;
    $csv_data[$i][] = $_POST['location'][$i];

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=file.csv');

    $output = fopen('php://output', 'w');
    fputcsv($output, array("Name", "Slope", "Length", "Size", "Max Length", "Location"));
    foreach ($csv_data as $value) {
        fputcsv($output, $value);
    }

This is the output which I was need.

Name    Slope   Length  Size    Max Length  Location
Name1    5       150    12"        500      location1 
Name2    8       350    12"        400      location12
Name3    16      326    12"        400      location3 
Name4    36      127    12"        400      location4 
Noor
  • 1,351
  • 8
  • 27
0

what is the value of PHP_EOL make it "\r\n" and try

$csv_data[] = $_POST['location'][$i].PHP_EOL; change to 
$csv_data[] = $_POST['location'][$i]."\r\n";
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73