Home  >  Article  >  Backend Development  >  php 导出数据,格式替csv

php 导出数据,格式替csv

WBOY
WBOYOriginal
2016-06-13 11:50:42756browse

php 导出数据,格式为csv
php 导出数据,格式为csv 不用phpexcel类,请问还有别的方法吗?自己写个类应该怎样写?多谢!
------解决方案--------------------

$fp = fopen('csv文件名', 'w');<br />$rs = mysql_query('select * from tbl_name');<br />while($row = mysql_fetch_assoc($rs) {<br />  fputcsv($fp, $row);<br />}<br />fclose($fp);

------解决方案--------------------
<br /><form enctype="multipart/form-data" action="import.php" method="POST"><br />    导入模板  <br />    <label for="文件选择">文件选择:</label><input name="csv_goods" type="file" /><br />    <input type="submit" value="导入" name="import" /><br /></form><br /><?php<br />if (isset($_POST['import'])){<br />    <br />    $file = $_FILES['csv_goods'];<br />    <br />    $file_type = substr(strstr($file['name'],'.'),1);<br />    <br />    // 检查文件格式<br />    if ($file_type != 'csv'){<br />        echo '文件格式不对,请重新上传!';<br />        exit;<br />    }<br />    $handle = fopen($file['tmp_name'],"r");<br />    $file_encoding = mb_detect_encoding($handle);<br />    <br />    // 检查文件编码<br />    if ($file_encoding != 'ASCII'){<br />        echo '文件编码错误,请重新上传!';<br />        exit;<br />    }<br />    <br />    $row = 0;<br />    while ($data = fgetcsv($handle,1000,',')){<br />        //echo "<font color=red>$row</font>";  //可以知道总共有多少行<br />        $row++;<br />        if ($row == 1)<br />        continue;<br />        $num = count($data);<br />        // 这里会依次输出每行当中每个单元格的数据<br />        for ($i=0; $i<$num; $i++){<br />            echo $data[$i]."<br>";<br />            // 在这里对数据进行处理<br />        }<br />    }<br />    <br />    fclose($handle);<br />}<br /><br />?> <br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn