หน้าเว็บ

Thursday, September 24, 2015

Export Excel จาก MySQL ด้วย PHP แบบง่ายๆ

เพียงนำโค๊ดด้านล่างนี้ไปใว้บนสุดของหน้าเว็บไซต์ที่ต้องการ Export และเปลี่ยนชื่อ file_name เป็นชื่อที่ต้องการ
<?php
header('Content-Type: text/html; charset=utf-8');
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="file_name.xls"');#file name
echo '<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">';
?>

ถ้าแสดงข้อมูลด้วย table
<TABLE  x:str BORDER="1">
คือตัวเลขที่ขึ้นต้นด้วย 0 ก็จะแสดงเลข 0 ด้วย โค๊ดข้างบนคือทำให้ตัวเลขกลายเป็นชนิดของ string

No comments:

Post a Comment