2012-10-18 20 views
1

我有一個調查網站,收集用戶輸入的數據並將其存儲在MySQL中。有沒有辦法使用ODBC來讓用戶能夠下載MySQL格式化的Excel文件?

我希望一些用戶能夠訪問允許他們下載數據的格式化excel文件的頁面(注意,不是csv)。

我聽說ODBC允許你與MySQL接口,但無法找到任何服務器端應用程序。

可能嗎?

我正在使用PHP的網站。

感謝

+2

嘗試使用PHPExcel。 – sephoy08

+0

如果你想給Excel動態訪問MySQL數據,你只需要建立一個ODBC連接。如果你對下載感到滿意,這是矯枉過正。關於連接Excel和MySQL的一些細節可以在這裏找到:http://helpdeskgeek.com/office-tips/excel-to-mysql/ – AllInOne

+0

感謝你們對這兩個建議 – Gideon

回答

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"); 

按計劃它應該工作。

編輯: 這裏有一個表,XLS與正確的XHTML模式的一個例子:

<?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> 
+0

我看,是否可以聲明格式要求使用這種方法?例如使列標題爲粗體 – Gideon

+0

是的,您可以使用css風格,無論如何你想要的。 – Tivie

+0

太棒了,謝謝你將這樣的努力帶入了一個詳細的答案! – Gideon

相關問題