1

我正在尝试将二维数组插入 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 文件的单行中。

有任何想法吗

4

2 回答 2

1

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

$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ($export_arr as $fields) {
    fputcsv($fp, $fields,",");
}
fclose($fp);
于 2013-08-13T05:53:48.927 回答
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:

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

输出:

沃尔沃 100 96
宝马 60 59
丰田 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
宝马
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);
?>
于 2013-08-13T06:11:42.167 回答