将数据从mysql数据库下载到php中的word文档

时间:2015-03-18 18:48:04

标签: php mysql database download

我正在尝试使用php将mysql数据库中保存的数据下载到word文档。

我现在的代码意味着如下工作, 显示数据库学生表中的所有数据 单击下载按钮,将此数据下载到Microsoft Word文档中。

但是,它仅在学生表的记录中打印出来,当您尝试打开文档时,它表示内容已损坏(使用谷歌浏览器时,它在Firefox中打开正常)。

非常感谢任何帮助。 (见下面的代码)

    <?php
include("dbConnect.php");
$dbQuery = $db->prepare("select * from student");
$dbQuery->execute();
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
    $ID                 = $dbRow['ID'];
    $Forename           = $dbRow['Forename'];
    $Surname            = $dbRow['Surname'];
    $Email              = $dbRow['Email'];
    $B_number           = $dbRow['B_number'];
    $School             = $dbRow['School'];
    $Campus             = $dbRow['Campus'];
    $Research_Institute = $dbRow['Research_Institute'];
    $FTPT               = $dbRow['FTPT'];
}
$doc_body = "<h1>Students</h1>";
?>

    <head>
    <title>Students</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"   href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">

    </head>

    <form name="export_form" action="<?php
echo ($_SERVER['PHP_SELF']);
?>" method="post">
    <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
    </form>

    <?php
if (isset($_POST['submit_docs'])) {
    header("Content-Type:application/msword");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("content-disposition: attachment;filename=test.docx");
}
echo "<html>";
echo "$doc_body";
echo "<table class=table table-striped table id=student>
                <tr>
                <th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
                </tr>
                  <tr><td>$ID</td><td>$Forename</td><td>$Surname</td> <td>$Email</td><td>$B_number</td><td>$School</td><td>$Campus</td><td>$Research_Institute</td><td>$FTPT</td>
                   </tr>;
            </table>";
echo "</html>";
?>

2 个答案:

答案 0 :(得分:1)

while循环中的代码会覆盖以前的值,因此最终会得到$ID$Forename,...中最后一个条目的值。您需要在while循环中生成输出。此外,您必须确保在生成单词内容时不要发送您的网站内容。

尝试这样的结构:

<?php 
include ("dbConnect.php");

function databaseOutput() {

  $dbQuery = $db->prepare("select * from student");
  $dbQuery->execute();

  while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
?>
    <tr>
      <td><?php echo $dbRow['ID']; ?></td>
      <td><?php echo $dbRow['Forename']; ?></td>
      <td><?php echo $dbRow['Surname']; ?></td>
      <td><?php echo $dbRow['Email']; ?></td>
      <td><?php echo $dbRow['B_number']; ?></td>
      <td><?php echo $dbRow['School']; ?></td>
      <td><?php echo $dbRow['Campus']; ?></td>
      <td><?php echo $dbRow['Research_Institute']; ?></td>
      <td><?php echo $dbRow['FTPT']; ?></td>
    </tr>
<?php    

  }

} // end of function databaseOutput()

if ($_POST['submit_docs']) { // word output

  header("Content-Type:application/msword");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("content-disposition: attachment;filename=test.docx");

?>
<html>
  <body>
    <h1>Students</h1>
    <table>
      <tr>
        <th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
      </tr>
      <?php databaseOutput(); ?>
    </table>
  </body>
</html>
<?php

  exit; // end of word output

}
?>
<html>
  <head>
    <title>Students</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"   href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">
  </head>
  <body>
    <form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
      <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
    </form>
    <table class="table table-striped" id="student">
      <tr>
        <th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
      </tr>
      <?php databaseOutput(); ?>
    </table>
  </body>
</html>

我没有对此进行测试,但与此类似的东西应该有用。

答案 1 :(得分:0)

我会将我的标题更改为:(未测试但应该可以使用)

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=test.doc");

同样在<html>代码和$doc_body之间添加此行:

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">

这应该会为您提供与更多浏览器兼容的旧版本,如果需要,它只会更新文件。希望这有帮助!

修改完整代码:

<?php
if(isset($_POST['submit_docs']))
 {
    header("Content-type: application/vnd.ms-word");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Disposition: attachment;Filename=test.doc");
 }

    $pri = "<html>";
    $pri .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    $pri .= $doc_body;
    $pri .= "<table class=table table-striped table id=student>";
    $pri .= "<tr>";
    $pri .= "<th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>";
    $pri .= "</tr>";
    $pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";
    $pri .= "</tr>";
    $pri .= "</table>";
    $pri .= "</html>";

echo $pri;
    ?>

通过这种方式,您可以转义所有空白区域,以及输出php变量,而不会冒被解释的广告文字的风险。

通过结果循环并将所有内容输入到词语文件中: 从文档顶部删除while循环并替换此行:

$pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";

用这个:

while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) 
{
        $ID       = $dbRow['ID'];
        $Forename = $dbRow['Forename'];
        $Surname  = $dbRow['Surname'];
        $Email  = $dbRow['Email'];
        $B_number = $dbRow['B_number'];
        $School = $dbRow['School'];
        $Campus = $dbRow['Campus'];
        $Research_Institute = $dbRow['Research_Institute'];
        $FTPT = $dbRow['FTPT'];

        $pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";

    }

应该像魅力一样工作:)