项目需要,由于系统工作日志数据量很大,所以定期将历史日志导出为Excel。


功能目录:

root@bupt925:/home/bupt/xuxueliang/OutputExcel# ll
total 24
drwxr-xr-x 6 root root 4096 10月 16 10:46 ./
d-wx-wx--x 9 1000 bupt 4096 10月 16 10:38 ../
drwxr-xr-x 2 root root 4096 10月 16 10:46 bin/
drwxr-xr-x 2 root root 4096 10月 16 10:36 conf/
drwxr-xr-x 2 root root 4096 10月 16 10:46 data/
drwxr-xr-x 2 root root 4096 10月 16 10:33 log/


功能代码:

$v)
    {
        $head[$i] = iconv('utf-8','gbk',$v);
    }
    fputcsv($fp,$head);
    $cnt = 0;
    $limit = 100000;
    while(($info = mysql_fetch_array($sqlf,MYSQL_NUM)))
    {
        $cnt ++ ;
        if($cnt == $limit)
        {
            $cnt = 0;
            ob_flush();
            flush();
        }
        foreach($info as $i=>$v)
        {
            $info[$i] = iconv('utf-8','gbk',$v);
        }
        fputcsv($fp,$info);
    }
    if($ifdelete)
    {
        $sql = "delete from Record_Log where date<'$date'";
        $sqlf = mysql_query($sql);
        if($sqlf)
            echo "Delete Success.\n";
        else
            echo "Delete Error.\n";
    }
    echo "Done\n";
?>


生成的csv文件:

root@bupt925:/home/bupt/xuxueliang/OutputExcel/data# ll
total 12
drwxr-xr-x 2 root root 4096 10月 16 10:47 ./
drwxr-xr-x 6 root root 4096 10月 16 10:46 ../
-rw-r--r-- 1 root root 5564 10月 16 10:47 Log_2013-09-16.csv