访客地图

php导出数据到xls文件

张成
2015/6

<?php
/**
 * 导出数据到xls。
 */
$db = mysql_connect("localhost",'root','');
if(mysql_errno()){
    exit(0);
}
mysql_select_db('test');
mysql_set_charset('utf8');
$res = mysql_query("select gindex,description_short,description_editor,description_rec from org_game_detail");
$fileName = 'detail.txt';
$fp = fopen($fileName,'a+');
$result = '';
if($res && mysql_affected_rows())
{
    fwrite($fp, "gindex\tdescription_short\tdescription_editor\tdescription_rec\r\n");
    while($result= mysql_fetch_assoc($res)){
        printf("gindex ----%d\n",$result['gindex']);
        foreach ($result as &$value) {
            $value = str_replace("\r\n", '\r\n', $value);
            $value = str_replace("\n", '\n', $value);
            $value = str_replace("\t", '\t', $value);
        }
        $str = join("\t",$result);
        $str .= "\r\n";
        fwrite($fp, $str);
    }
}
fclose($fp);
$path = dirname(__FILE__);
system("ren $path\\$fileName detail.xls");