如何將網頁表單下載成Excel?
透過PHP header設定將網頁表單下載成Excel.
<?php
header('Content-type:application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=myTest.xls');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.text {
mso-number-format:\@;
}
</style>
</head>
<body>
<table border="1">
<tr>
<td>Title 1</td>
<td>Title 2</td>
<td>Title 3</td>
<td>Title 4</td>
</tr>
<tr>
<td class="text">02224567</td>
<td>a2</td>
<td>a3</td>
<td>a4</td>
</tr>
<tr>
<td class="text">02113456</td>
<td>b2</td>
<td>b3</td>
<td>b4</td>
</tr>
<tr>
<td class="text">0144789</td>
<td>c2</td>
<td>c3</td>
<td>c4</td>
</tr>
<tr>
<td class="text">0133456</td>
<td>c2</td>
<td>c3</td>
<td>c4</td>
</tr>
</table>
</body>
</html>
因為A欄套用CSS強制轉文字,因此轉成excel就會變成文字格式.
另外資料量太大可能出現下方錯誤資訊:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes) in XXXX
可以在程式最上方加入語法,來修改記憶體的上限.
ini_set('memory_limit', '2048M');
補充:
也可以透過fopen, fwrite產生excel並將資料寫進去.
留言
張貼留言