使用PHP将多行格式化为CSV格式

时间:2017-06-09 17:03:28

标签: php csv export-to-csv carriage-return

    $csvFile = [];
    $firstRow = ['#','Freq','Mode','Shift','Offset','TX Freq','Enc/Dec','Tone','Code','Show','Name','Power','Scan','Clk','Step','Scan2','Scan3','Scan4','Scan5','Scan6','Description','Bank1','Bank2','Bank3','Bank4','Bank5','Bank6','Bank7','Bank8','Bank9','Bank10','Bank11','Bank12','Bank13','Bank14','Bank15','Bank16','Bank17','Bank18','Bank19','Bank20','Bank21','Bank22','Bank23','Bank24','PRFRQ','SMSQL','RXATT','BELL','Masked','Internet','DCSinv'];
    array_push($csvFile, $firstRow);

    while($row = mysqli_fetch_array($result)) {
    $element = [count($csvFile), $RX, $modulation, $offsetDirection, $offsetFrequency, '', $toneMode, $CTCSS, '023', '', $row['Repeater'].'-'.$row['Sign'], 'HIGH', 'Off', 'OFF', "12.5 K", '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '1600HZ', 'OFF', 'OFF', 'OFF', 'NO', 'OFF', 'RN-TN'];
    array_push($csvFile, $element);
    }

    function array_to_csv_download($array, $filename = "FTBVX8DE.csv", $delimiter=";") {
    $f = fopen('php://memory', 'w'); 
    foreach ($array as $line) { 
        fputcsv($f, $line, $delimiter);
    }
    fseek($f, 0);
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'";');
    fpassthru($f);
    }

    array_to_csv_download($csvFile);

大家好,我确实需要你的帮助。上面是一段代码,它创建一个CSV文件,用于导入火腿无线电操作员的特定软件中的数据。显然,如果我在Excel中打开文件,输出看起来正确,列中有相应的字段/值。

但是当我尝试在软件中导入数据时,会发生错误。在记事本中打开CSV文件我注意到我的数据直接显示在一行中: wrong formatting without a pseudo carriage return

相反,成功导入的格式应为:

correct formatting must be exactly like this

现在,我尝试了几种方法在StackOverflow中完成此任务搜索,例如插入PHP_EOL,但没有成功。

有任何帮助/建议吗?非常感谢!

0 个答案:

没有答案