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

标签 php mysql excel

我目前正在为我的学校开发一个应用程序来记录每个类(class)的类(class)清洁度结果,因此我需要使用 PHP 将 MySQL 表格中整理的结果转换为 Microsoft Excel,最好也能够通过 Android 操作系统手机打开。

我使用了以下 PHP 代码:

<?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) . "\n";
}
mysqli_close($link);
?>

这是我的表格在 phpMyAdmin 中的样子:

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

这是我使用此代码转换后 Excel 文件的样子。

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

为什么没有列和行?我需要 Excel 文件的格式和样式与 phpMyAdmin 中的表格完全相同。任何人都可以帮助编辑我的代码而不是为我提供全新的代码吗?

预先感谢您的回答!

最佳答案

手动

  1. 运行本地主机并登录 phpMyAdmin
  2. 点击您的数据库,然后点击您想要获取的表 Excel。
  3. image

然后

  • enter image description here
  • 然后按“GO”按钮
  • 使用代码

    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");
    

    然后

    $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)."\n";
    }
      $setData = str_replace("\r", "", $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)."\n".$setData."\n";
    

    More About Code

    关于php - 如何使用 PHP 将 MySQL 表导出到 Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30906142/

    相关文章:

    php - 在 PHP 中为空数据库查询生成错误消息

    php - 显示从星期一开始的周报告

    php - 尝试在 Smarty 中获取表和数据库数组

    mysql - 您可以将 AdminBro 用作 MySQL 和 express.js 的管理面板吗?

    php - mysql中的字符串匹配函数

    vba - Excel 2007 vbe 无法识别重音字符

    php - 我的标记技术是否正确?

    php - 网页无法读取 CSS 文件

    vba - 当存储在集合中时,如何更改类属性的值

    excel - 动态超链接功能,用于创建从一个单元格到另一个单元格的链接