如何使用php将mysql数据库导出到excel,其中一些列不是一般的,而是作为文本格式化。
我希望列SNumber和Phone格式化为文本。 id,fname和lname很好......
我的php看起来像这样:
<?php include "db.php"; ?>
<?php ob_start(); ?>
<?php
$output = "";
if(isset($_POST['export_excel'])) {
$sql = mysqli_query($connection, "SET NAMES utf8;");
$sql = "SELECT * FROM table";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0) {
$output .= "
<table class='table' bordered='1'>
<tr>
<th>ID</th>
<th>FName</th>
<th>LName</th>
<th>SNumber</th>
<th>Phone</th>
</tr>
";
while($row = mysqli_fetch_array($result)) {
$output .= "
<tr>
<td>".$row['id']."</td>
<td>".$row['fname']."</td>
<td>".$row['lname']."</td>
<td>".$row['snumber']."</td>
<td>".$row['phone']."</td>
</tr>
";
}
$output .= "</table>";
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=kapCMS.xls");
echo $output;
}
}
?>