在php中的csv文件中插入一个2d数组

时间:2013-08-13 05:48:27

标签: php arrays multidimensional-array

我正在尝试将2d数组插入csv文件

这是我的代码

<?php
$cars = array( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100));
$fp = fopen('file.xls', 'w');
foreach ($cars as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);
?>

但是这些值在csv文件中以单行形式插入。

任何想法

2 个答案:

答案 0 :(得分:1)

您需要使用逗号分隔内容以在单独的行中写入。

$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ($export_arr as $fields) {
    fputcsv($fp, $fields,",");
}
fclose($fp);

答案 1 :(得分:1)

编辑1:

此版本将其保存到文件而不提示在带有轻微边框的表格中保存和输出。表中的边框不会写入文件,只会输出到屏幕。

<?php

$fp = fopen('file.xls', 'w');
$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
$titleArray = array_keys($cars[0]);
$delimiter = "\t";
$filename="file.xls";

//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
    //Separate each datapoint in the row with the delimiter
    $dataRowString = implode($delimiter, $subArray);

//  print $dataRowString . "\r\n"; // prints output to screen

fwrite($fp, $dataRowString . "\r\n");

} // keep this always end of routine

// start of cell formatting

$row = 1;
if (($handle = fopen("file.xls", "r")) !== FALSE) {

    echo '<table border="1" cellspacing="0" cellpadding="3">';

    while (($data = fgetcsv($handle, 1000, '\t')) !== FALSE) {
        $num = count($data);
        if ($row == 1) {
            echo '<thead><tr>';
        }else{
            echo '<tr>';
        }

        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if(empty($data[$c])) {
               $value = "&nbsp;";
            }else{
               $value = $data[$c];
            }
            if ($row == 1) {
                echo '<th>'.$value.'</th>';
            }else{
                echo '<td align="center">'.$value.'</td>';
            }
        }

        if ($row == 1) {
            echo '</tr></thead><tbody>';
        }else{
            echo '</tr>';
        }
        $row++;
    }

    echo '</tbody></table>';
    fclose($handle);
}

?>


编辑2:

此版本将处理数据,然后提示保存文件。

<强>输出:

Volvo   100 96
BMW 60  59
Toyota  110 100

PHP代码:

<?php

$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));

$titleArray = array_keys($cars[0]);

$delimiter = "\t";

$filename="file.xls";

//Send headers
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");


//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
    //Separate each datapoint in the row with the delimiter
    $dataRowString = implode($delimiter, $subArray);
    print $dataRowString . "\r\n";
}
?>


第一版:

输出将是:

沃尔沃
100个
96个
BMW
60个
59个
丰田
110个
100

如果这是期望的结果,那么这是完成此任务的代码:

<?php

$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));

$fp = fopen('file.xls', 'w');

foreach ($cars as $fields) {

    fputcsv($fp, $fields, "\n");

}
fclose($fp);
?>