如果是直接导出到EXCEL的话,依目前的技术能力似乎还不能做到。连大名鼎鼎的OpenOffice对EXCEL的支持也不是很好。
不过有种投机取巧的方法可以实现,就是从数据库中取出数据后,生成一个表格,样式自己定义好,然后让浏览器识别为下载excel。以下只是一个示例:<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");$conn = @mysql_connect("localhost", "root", "") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<table border="1" cellspacing="2" cellpadding="2" width="50%" align="center">';
// 输出标题
echo '<tr bgcolor="#cccccc"><td colspan="3" align="center">' . $title . '</td></tr>';$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
// 输出字段名
echo '<tr bgcolor="blue">';
for($i = 0; $i < $fields; $i++) {
echo '<td>' . mysql_field_name($result, $i) . '</td>';
}
echo '</tr>';
// 输出内容
while($row = mysql_fetch_row($result)) {
echo '<tr>';
for($i = 0; $i<$fields; $i++) {
echo '<td>' . $row[$i] . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
不过有种投机取巧的方法可以实现,就是从数据库中取出数据后,生成一个表格,样式自己定义好,然后让浏览器识别为下载excel。以下只是一个示例:<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");$conn = @mysql_connect("localhost", "root", "") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<table border="1" cellspacing="2" cellpadding="2" width="50%" align="center">';
// 输出标题
echo '<tr bgcolor="#cccccc"><td colspan="3" align="center">' . $title . '</td></tr>';$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
// 输出字段名
echo '<tr bgcolor="blue">';
for($i = 0; $i < $fields; $i++) {
echo '<td>' . mysql_field_name($result, $i) . '</td>';
}
echo '</tr>';
// 输出内容
while($row = mysql_fetch_row($result)) {
echo '<tr>';
for($i = 0; $i<$fields; $i++) {
echo '<td>' . $row[$i] . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
如果行,则用php就行
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name></x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]--> </head><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td align="center" class="big6" >Users (except the ones disabled)</td></tr>
<tr><td class="greyborder">
<table border="1" align="center" width="100%" cellpadding="1" cellspacing="1">
<tr align="center">
<td class="TableTopHeader" nowrap>
Login </td>
<td class="TableTopHeader" nowrap>
Name </td>
<td class="TableTopHeader" width="3%" nowrap>
EMail </td>
<td class="TableTopHeader" nowrap>
Department </td>
<td class="TableTopHeader" nowrap>
Position </td>
<td class="TableTopHeader" nowrap width="1%">
Level </td>
<td class="TableTopHeader">Address</td>
<td class="TableTopHeader">Phone</td>
</tr>
<tr class="even">
<td class="mtlist"> joesen</td>
<td class="mtlist"> hong joesen</td>
<td class="mtlist" >[email protected]</td>
<td class="mtlist"> Software developments</td>
<td class="mtlist"> System Administrator</td>
<td class="mtlist"> Admin</td>
<td class="mtlist"> </td>
<td class="mtlist"> </td>
</tr>
</table>
</td></tr>
</table>
这就是 phpteam(我忍住不哭) 提供的方法不错,不错。有长见识了!
下载下来的.xls文件,你用文本编辑器打开就是那个样子。你用Excel打开就可以进行编辑。我理解他是一个Excel可识别处理的xml文档,不是真正的Excel文档。但这已经很好了。