dougudu3564 2015-06-18 03:49
浏览 46
已采纳

如何使用PHP将MySQL表导出到Excel?

I am currently developing an App for my school to record Class Cleanliness Results for each class, so I need to convert the results collated in MySQL table into Microsoft Excel using PHP, preferably also able to be opened by a Android OS Phone.

I used the following PHP code:

<?PHP

$mysqli_user = "(user)";
$mysqli_password = "(password)";
$mysqli_host = "(host)";
$mysqli_database = "(database)";


  $filename = "grading_results_" . time() . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

$link = mysqli_connect($mysqli_host,$mysqli_user,$mysqli_password,$mysqli_database);
$query = 'SELECT * FROM (table_name)';

$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_row($result)){
 print implode("\t", $row) . "
";
}
mysqli_close($link);
?>

This is how my table looks like in phpMyAdmin:

https://www.dropbox.com/s/7pr3gh06zta5d8u/Snip20150618_2.png?dl=0

This is how the Excel file looks like after I used this code to convert.

https://www.dropbox.com/s/571m9lfj64tklpc/Snip20150618_3.png?dl=0

Why is there no columns and rows? I need the Excel file to be exactly the same formatting and style as the table in phpMyAdmin. Can anyone help edit my code instead of providing me a brand new code?

Thanks in advance for your answers!

  • 写回答

3条回答 默认 最新

  • douyan2470 2015-06-18 03:54
    关注

    Manual

    1. Run tour localhost and log in to phpMyAdmin
    2. Click on your database then click on the table which you want to get Excel.
    3. image

    then

    1. enter image description here
    2. then press GO button

    With Code

    define ("DB_HOST", "localhost");
    define ("DB_USER", "root");
    define ("DB_PASS","");
    define ("DB_NAME","DATABASE_NAME");
    
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
    $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
    

    then

    $setCounter = 0;
    
    $setExcelName = "download_excal_file";
    
    $setSql = "YOUR SQL QUERY GOES HERE";
    
    $setRec = mysql_query($setSql);
    
    $setCounter = mysql_num_fields($setRec);
    
    for ($i = 0; $i < $setCounter; $i++) {
        $setMainHeader .= mysql_field_name($setRec, $i)."\t";
    }
    
    while($rec = mysql_fetch_row($setRec))  {
      $rowLine = '';
      foreach($rec as $value)       {
        if(!isset($value) || $value == "")  {
          $value = "\t";
        }   else  {
    //It escape all the special charactor, quotes from the data.
          $value = strip_tags(str_replace('"', '""', $value));
          $value = '"' . $value . '"' . "\t";
        }
        $rowLine .= $value;
      }
      $setData .= trim($rowLine)."
    ";
    }
      $setData = str_replace("", "", $setData);
    
    if ($setData == "") {
      $setData = "no matching records found";
    }
    
    $setCounter = mysql_num_fields($setRec);
    
    
    
    //This Header is used to make data download instead of display the data
     header("Content-type: application/octet-stream");
    
    header("Content-Disposition: attachment; filename=".$setExcelName."_Report.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    //It will print all the Table row as Excel file row with selected column name as header.
    echo ucwords($setMainHeader)."
    ".$setData."
    ";
    

    More About Code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么