zoukankan      html  css  js  c++  java
  • PHP将mysql数据表转换为excel文件

    测试代码:

    <?php
    $DB_Server = "127.0.0.1";
    $DB_Username = "root";
    $DB_Password = "";
    $DB_DBName = "parsersample";
    $DB_TBLName = "sampledata";

    $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
    $Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database.");
    $file_type = "vnd.ms-excel";
    $file_ending = "xls";

    header("Content-Type: application/$file_type;charset=utf-8");
    header("Content-Dis; filename=backup.$file_ending");
    header("Pragma: no-cache");
    header("Expires: 0");

    $now_date = date('Y-m-d H:i');
    $sql = "Select * from $DB_TBLName";
    $ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database");
    $result = @mysql_query($sql,$Connect) or die(mysql_error());

    $sep = " ";
    for ($i = 0; $i < mysql_num_fields($result); $i++) {
    echo mysql_field_name($result,$i) . " ";
    }
    print(" ");
    $i = 0;
    while($row = mysql_fetch_row($result))
    {
    $schema_insert = "";
    for($j=0; $j<mysql_num_fields($result);$j++)
    {
    if(!isset($row[$j]))
    $schema_insert .= "NULL".$sep;
    elseif ($row[$j] != "")
    $schema_insert .= iconv("utf-8","gb2312","$row[$j]").$sep;
    else
    $schema_insert .= "".$sep;
    }
    $schema_insert = str_replace($sep."$", "", $schema_insert);
    $schema_insert .= " ";
    print(trim($schema_insert));
    print " ";
    $i++;
    }

    return (true);
    ?>

    个人作品展示:www.jinyuanbao.cn
  • 相关阅读:
    Eclipse安装SVN插件(转载)
    推荐eclipse插件Properties Editor(转)
    Eclipse快捷键(转载)
    添加路由命令
    oracle 随机数(转载)
    《图解CSS3——第1章 揭开CSS3的面纱》
    css高度自适应
    JS生成随机数
    判断IE浏览器版本
    IE下SCRIPT5009: “JSON”未定义
  • 原文地址:https://www.cnblogs.com/jyb2014/p/3977409.html
Copyright © 2011-2022 走看看