การส่งออกเข้า Ms Excel อย่างง่ายๆ จัดทำโดย : first
 Untitled Document

การส่งออกเข้า Ms Excel อย่างง่ายๆ

การส่งออกเข้า Ms Excel จะส่งออกในรูปแบบของไฟล์ .csv และสามารถเปิดด้วย โปรแกรม Microsoft Excel มาดูตัวอย่าง code กันเลย

1.เริ่มแรกให้สร้าง ฐานข้อมูลชื่อ mydatabase รายละเอียดของตาราง

// สร้างตาราง ชื่อ testing

CREATE TABLE `testing` (
`id` int(3) NOT NULL auto_increment,
`question` varchar(100) NOT NULL default '',
`c1` varchar(100) NOT NULL default '',
`c2` varchar(100) NOT NULL default '',
`c3` varchar(100) NOT NULL default '',
`c4` varchar(100) NOT NULL default '',
`answer` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;

#
// แทรกข้อมูลลงตาราง testing
# dump ตาราง `testing`
#

INSERT INTO `testing` VALUES (1, 'ข้อที่ 1', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 1);
INSERT INTO `testing` VALUES (2, 'ข้อที่ 2', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (3, 'ข้อที่ 3', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);
INSERT INTO `testing` VALUES (4, 'ข้อที่ 4', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 4);
INSERT INTO `testing` VALUES (5, 'ข้อที่ 5', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 4);
INSERT INTO `testing` VALUES (6, 'ข้อที่ 6', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);
INSERT INTO `testing` VALUES (7, 'ข้อที่ 7', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (8, 'ข้อที่ 8', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 1);
INSERT INTO `testing` VALUES (9, 'ข้อที่ 9', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (10, 'ข้อที่ 10', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);

2.สร้างไฟล์ sample1.php โดยพิมพ์ code ตามด้านล่างนี้

<?
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="testing.csv"');

$host="localhost";
$username="";
$password="";
$db="mydatabase";
$tb="testing";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");

echo "ข้อที่,คำถาม,ตัวเลือกที่ 1,ตัวเลือกที่ 2,ตัวเลือกที่ 3,ตัวเลือกที่ 4,ตัวเลือกที่ถูก,\n";
$sql = "select * from $tb";
$dbquery = mysql_query($sql);
$num_rows = mysql_num_rows($dbquery);

$i=0;
while ($i < $num_rows)
{
$result= mysql_fetch_array($dbquery);

echo "$result[id],$result[question],$result[c1],$result[c2],$result[c3],$result[c4],$result[answer],\n";

$i++;
}
?>

ผลลัพธ์ที่ได้

ที่มา :http://www.thaicreate.com

คุณอาจสนใจ
การใส่สีตัวอักษร และสีพื้นหลังของตัวอักษร
Mr.GuruZ (25,968 - 15 มี.ค. 51)
Head Modeling Using NURBS
เว็บไทยดีดี (31,113 - 04 เม.ย. 51)
ฟังก์ชั่นคืออะไร
สายลม (37,443 - 15 มี.ค. 51)
การสร้างตารางนัดหมายแบบง่าย ๆ
first (90,834 - 23 ธ.ค. 50)
การทำข้อความวิ่งตามเมาส์
Mr.GuruZ (31,623 - 26 พ.ย. 50)
การสร้าง Function แบบไม่ส่งค่า
Mr.GuruZ (26,670 - 03 มิ.ย. 49)
เทคนิคการทำภาพ Sketch
Mr.GuruZ (83,582 - 22 ก.ย. 50)
การย่อ-ขยายเอกสาร
นายนุก (23,162 - 22 มี.ค. 51)