PHP Gen Words Doc file แบบใช้ Template
ตัวอย่างการ Gen ไฟล์ word ออกมาเป็น .Doc แบบใช้ Template
ผมทำตามขึ้นตอนนี้ครับ
ตัวอย่าง template.doc พิมพ์ remark ไว้ เช่น
#data1
#data2
จากนั้นใน PHP ก็ Code ตามนี้เลย
ตัวอย่าง gen_doc.php
$template_file = "template.doc";
$handle = fopen($template_file , "r");
//เปิดไฟล์ template ขึ้นมา
$contents = fread($handle, filesize($template_file));
//ทำการ Replace ค่าตามจุดที่เรา ทำ Remark ไว้
$original= array("#data1", "#data2");
$new = array("1.xxxxxxx\n2.bbbbbbbb\n3.xxxxxxxxx", "Good Job");
$newphrase = str_replace($original, $new, $contents);
//สั่ง Export ออกมาเป็นชื่อไฟล์ใหม่
$handle2 = fopen("test.doc" , "w");
echo $newphrase;
fwrite ( $handle2 ,$newphrase);
fclose ($handle);
fclose($handle2);
ผลที่ได้ไฟล์ test.doc คือ
1.xxxxxxx
2.xxxxxxxx
3.xxxxxxxxxx
Good Job
แบบง่ายๆ ก็ประมาณนี้ครับ