1

我有一个调查网站,它从用户输入中收集数据并将其存储在 MySQL 中。

我希望一些用户能够访问允许他们下载数据的格式化 excel 文件的页面(注意,不是 csv)。

我听说 ODBC 允许您将 excel 与 MySQL 连接,但找不到任何服务器端应用程序。

可能吗?

我正在为该网站使用 PHP。

谢谢

4

1 回答 1

2

将数据库导出到 excel 文件应该不难,因为 excel 可以读取 xhtml。

只需将您的信息输出为常规 HTML 表格

<table>
<tr>
<td>title</td>
</tr>
<tr>
<td>record1</td>
</tr>
</table>

然后添加这些标题以强制自动下载和解释为 excel 文件:

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=\"{$yourFileName}\"" );
header ("Content-Description: PHP Generated Data" );

它应该按预期工作。

编辑:这是具有正确 XHTML 模式的 xls 表的示例:

<?php
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=newtest.xls" );
header ("Content-Description: PHP Generated Data" );
?>
<html xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name="Excel Workbook">
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 14">
<style>
#test
{
    font-style:italic;
}
</style>
</head>
<body>
<table>
    <tr>
        <td><b>header in bold</b></td>
        <td><i>header in italic</i></td>
    </tr>
    <tr>
        <td>1</td>
        <td><span style="font-weight:bold; font-style:italic">my data with css styling</span></td>
    </tr>
    <tr>
        <td>2</td>
        <td><span id="test">I'm italic 'cause I can read style</span></td>
    </tr>
</table>
</body>
</html>
于 2012-10-18T01:04:09.913 回答