将 mysql 数据库中的数据下载到 php 中的 word 文档
Download data from mysql database to a word document in php
我正在尝试使用 php 将 mysql 数据库中保存的数据下载到 word 文档。
我现在的代码是按如下方式工作的,
显示数据库student table内的所有数据
单击下载按钮,将此数据下载到 Microsoft word 文档中。
但是,它只是打印出学生 table 的记录,当您尝试打开文档时,它说内容已损坏(使用 google chrome 时,它在 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>";
?>
我会将我的 headers 更改为:(未测试但应该有效)
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;
?>
通过这种方式,您将转义所有白色 space,并输出 php 变量,而不用冒被解释的广告文本的风险。
遍历结果并将它们全部输出到 Word 文件中:
从文档顶部删除 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>";
}
应该很有魅力:)
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>
我没有对此进行测试,但类似的东西应该可以工作。
我正在尝试使用 php 将 mysql 数据库中保存的数据下载到 word 文档。
我现在的代码是按如下方式工作的, 显示数据库student table内的所有数据 单击下载按钮,将此数据下载到 Microsoft word 文档中。
但是,它只是打印出学生 table 的记录,当您尝试打开文档时,它说内容已损坏(使用 google chrome 时,它在 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>";
?>
我会将我的 headers 更改为:(未测试但应该有效)
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;
?>
通过这种方式,您将转义所有白色 space,并输出 php 变量,而不用冒被解释的广告文本的风险。
遍历结果并将它们全部输出到 Word 文件中: 从文档顶部删除 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>";
}
应该很有魅力:)
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>
我没有对此进行测试,但类似的东西应该可以工作。