我写的PHp网页里有一个查询数据库后得到结果的表格,现在要添加一个按钮,用户点击后可以将此表格下载为csv格式的文档,(此后用户可以用openoffice打开) , 找了很多资料都没成功,脑袋要爆炸了,我知道这是一个很简单的问题,但是因为我是计算机初学者,所以希望各位大侠帮帮我, 下面是我试用的一中方法, 能生成csv 文档, 但是里面都是乱七八糟的东西,没有我表格的内容。 谢谢大家了!!!HTML    echo"<div id=\"centrefournis\"><table>";
                echo"<tr><td bgcolor=skyblue>Nom du projet</td><td bgcolor=skyblue>Nombre de demandes de travaux</td></tr>";
                for($i=0, $t=0; $i < $nb_ft ; ++$i )
                {
                        echo"<tr>
                        <td class=\"bgc\">{$the_Liste[$i]['NOM_PROJ']}</td>
                        <td class=\"bgc\">{$the_Liste[$i]['SUM(NUM_DOC)']}</td>";
                 echo"</tr>";
                $t=$t+$the_Liste[$i]['SUM(NUM_DOC)'];
                }
                echo"<tr><td bgcolor=skyblue><strong>TOTAL</strong></td><td bgcolor=skyblue><strong>$t</strong></td></tr>";
                echo"<tr><td><strong>Vue Graphique</strong></td><td></td></tr>";
                echo"</table><a href=\"../Php/dl.php\">T&eacute;l&eacute;chargerSauvegarder csv</a><div>";
DL.PHP<?php
header("Content-disposition: attachment; filename=monfichier.csv");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: text/plain\n");
header("Content-Length: ".filesize('monfichier.csv'));
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");
readfile('monfichier.csv');
?>
~生成csv 文档都 是乱七八糟的东西,没有我表格的内容;,;,,,,,,,,,,,,,,,,,,,,,,,,<br />
<b>Warning</b>:  filesize(): Stat failed for monfichier.csv (errno=2 - No such file or directory) in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /rtm/chen/cwork/fengjao-work/Php/dl.php:5) in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /rtm/chen/cwork/fengjao-work/Php/dl.php:5) in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /rtm/chen/cwork/fengjao-work/Php/dl.php:5) in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /rtm/chen/cwork/fengjao-work/Php/dl.php:5) in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  readfile(monfichier.csv): failed to open stream: No such file or directory in <b>/rtm/chen/cwork/fengjao-work/Php/dl.php</b> on line <b>9</b><br />helphelpppppppppppppppppppppppppppppppppppppppppppppppp

解决方案 »

  1.   

    csv文档是以逗号分隔开来的数据集合
      

  2.   


    i know this, but i don't know how to use ............  do you have any idea??  please tell me...... 
      

  3.   

    暂且不论csv的格式  用html的tag去画文本文件的表格?
      

  4.   


    this is a part of my script, i write it in php, for understand better, i write here html......
      

  5.   


    do you have any idea?       i work in a logical.... write the webpage  with php  but it can change to web..... so i think it's like html.. so ... thank you for your answer..  help helppppppppppppppppppppp    
      

  6.   


    da ge, wo dou yao si le, bu yao kai wo wanxiao le .....................;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      

  7.   

    应该是你的csv文件生成有问题。
    你现在用的是HTML的表格,那索性就生成XLS好了。
    浏览器应该是支持的。
      

  8.   


    my boss need .csv 
      

  9.   

    do you know how to do?? please help me..
      

  10.   

    Please try these codes:
    <?php
    $column="order,name,gender,age,memo\r\n";
    $data="1,Jim,1,20,\r\n2,Tom,1,20,\r\n3,Kate,0,20,\r\n";
    header("Content-length: ".strlen($column.$data));//文件长度
    header("Content-Disposition: attachment;filename=".time().".csv");//保存的文件名
    header("Content-type: application/octet-stream");
    header("Content-Type: application/force-download");
    echo $column.$data;
    ?>
      

  11.   

      header('Pragma: public');
                header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
                header('Cache-Control: no-store, max-age=0, no-cache, must-revalidate'); // HTTP/1.1
                header('Cache-Control: post-check=0, pre-check=0', false);
                header('Cache-Control: private');
                header('Content-Transfer-Encoding: none'); 
                header('Content-Disposition: attachment; filename='.$filename.'');
                header('Content-Type: application/vnd.ms-excel; name="'.$filename.'"');
      

  12.   

    那些明明是程序产生了错误,你非要说是乱七八糟的东西!!
    从filesize那里就错了,发生错误以后,PHP就抛出了错误信息出来。
    后面的header就都跟着错了,因为header前不能有输出嘛,
      

  13.   


    it's very good, but i don't know how it can connect with my table( the table is already existed in my page) , and the table have many <tr></tr>, how can it take actively............................
    your example is only write the information in php, but it don't connect with my table which i want download............  i am so stupid!
      

  14.   


    ye, you are right, i am stupid. 
      

  15.   

    please check if this file is existed:monfichier.csv
    the csv file cannot include any HTML tags,I tested just now.
      

  16.   

    is ok   i have finded a good solution,in my page, 
    i take the data with the solution like this 
    $csv_output .= $row['orders_id'] . ", ";
    $csv_output .= $row['value'] . "\n";after i check all data, i add this
    <form name="export" action="export.php" method="post">
    <input type="submit" value="Export table to CSV">
    <input type="hidden" value="<? echo $csv_hdr; ?>" name="csv_hdr">
    <input type="hidden" value="<? echo $csv_output; ?>" name="csv_output">
    </form>
    and i creat a dl.php for downland<?php
    $out = ' ';
    if (isset($_POST['csv_hdr'])) {
            $out .= $_POST['csv_hdr'];
            $out .= "\n";
    }if (isset($_POST['csv_output'])) {
            $out .= $_POST['csv_output'];
    }if (isset($_POST['csv_hdr2'])) {
            $out .= $_POST['csv_hdr2'];
            $out .= "\n";
    }
    $filename = $file.date("Y-m-d_H-i",time());
    header("Content-Disposition: attachment;filename=".$filename.".csv");
    header("Content-Type: application/force-download");
    header("Content-Type: application/vnd.ms-excel");
    header("Content-type: application/octet-stream");
    echo $out;
    exit;
    ?>thanks god...