|    
         
        
           
            |   | 
            
			 
   
     
        PHPWord Library สร้างไฟล์ Word Document ด้วย PHP (Word 2003 , Word 2007)       | 
   
 
			
  PHPWord Library สร้างไฟล์ Word Document (doc,docx) ด้วย PHP (Word 2003 , Word 2007)  ก่อนหน่้านี้ได้ก็ review library ของ PHPExcel ไปแล้ว 1-2 บทความ ถึงความสามารถและความง่ายต่อกการใช้งาน และบทความนี้จะแนะนำ PHPWord เป็น Library น้องใหม่ที่เกิดไม่ยังไม่ถึงปีและอยู่ในช่วง Version Beta ให้ดาวน์โหลดไปทดลองใช้กัน  
 
  
 
คุณสมบัติโดดเด่นก็คือ ใช้งานได้ง่าย รองรับการทำงานได้ทั้ง Windows และ Linux Server ไม่ต้องอาศัยการทำงานผ่าน COM Word.Application และยังรองรับการทำงานได้ทั้ง Word ใน Version ของ Office 2003 (.doc) และ Office 2007 (.docx) 
 
 
Screenshot 
 
  
 
 
Download LiBrary PHPWord 
 
 
เมื่อดาวน์โหลดและแตกไฟล์ออกมา จะมีโฟเดอร์ Examples ซึ่งเป้นตัวอย่างการใช้งานในรูปแบบต่าง ๆ  
 
ตัวอย่างที่ 1 สร้างไฟล์ Word แบบง่าย ๆ  
 
CreateWord1.php 
<?php
	require_once 'PHPWord.php';
	// New Word Document
	$PHPWord = new PHPWord();
	// New portrait section
	$section = $PHPWord->createSection();
	// Add text elements
	$section->addText('Hello World!');
	$section->addTextBreak(2);
	$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
	$section->addTextBreak(2);
	$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
	$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
	$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
	$section->addText('I have only a paragraph style definition.', null, 'pStyle');
	// Save File
	$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
	$objWriter->save('CreateWord1.docx');
?>
 
 
Screenshot 
 
  
 
ตัวยอ่างแรกจะเป็นการเขียนข้อความลงในไฟล์ Word Document แบบง่าย ๆ  
 
 
 
 
ตัวอย่างที่ 2 ทดสสอบการส่งออกจากฐานข้อมูล MySQL Database 
 
ให้สร้างตารางดังนี้ 
CREATE TABLE `customer` (
  `CustomerID` varchar(4) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `CountryCode` varchar(2) NOT NULL,
  `Budget` double NOT NULL,
  `Used` double NOT NULL,
  PRIMARY KEY  (`CustomerID`)
) ENGINE=MyISAM;
-- 
-- Dumping data for table `customer`
-- 
INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John  Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);
 
 
นำไปสร้างบน phpMyAdmin 
 
 
  
 
โครงสร้างตารางและข้อมูล 
 
CreateWord2.php 
<?php
	require_once 'PHPWord.php';
	// New Word Document
	$PHPWord = new PHPWord();
	// New portrait section
	$section = $PHPWord->createSection();
	$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
	$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
	$section->addText('Customer Report', 'rStyle', 'pStyle');
	// Define table style arrays
	$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
	$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
	// Define cell style arrays
	$styleCell = array('valign'=>'center');
	$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
	// Define font style for first row
	$fontStyle = array('bold'=>true, 'align'=>'center');
	// Add table style
	$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
	// Add table
	$table = $section->addTable('myOwnTableStyle');
	// Add row
	$table->addRow(200);
	// Add cells
	$table->addCell(1500, $styleCell)->addText('CustomerID', $fontStyle);
	$table->addCell(1500, $styleCell)->addText('Name', $fontStyle);
	$table->addCell(1500, $styleCell)->addText('Email', $fontStyle);
	$table->addCell(1500, $styleCell)->addText('CountryCode', $fontStyle);
	$table->addCell(1500, $styleCell)->addText('Budget', $fontStyle);
	$table->addCell(1500, $styleCell)->addText('Used', $fontStyle);
	// Write data from MySQL result
	$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
	$objDB = mysql_select_db("mydatabase");
	$strSQL = "SELECT * FROM customer";
	$objQuery = mysql_query($strSQL);
	while($objResult = mysql_fetch_array($objQuery))
	{
			// Add more rows / cells
			$table->addRow();
			$table->addCell(1500)->addText($objResult["CustomerID"]);
			$table->addCell(1500)->addText($objResult["Name"]);
			$table->addCell(1500)->addText($objResult["Email"]);
			$table->addCell(1500)->addText($objResult["CountryCode"]);
			$table->addCell(1500)->addText($objResult["Budget"]);
			$table->addCell(1500)->addText($objResult["Used"]);
		}
	// Save File
	$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
	$objWriter->save('CreateWord2.docx');
?>
 
 
Screehshot 
 
  
 
จากรูปจะเป็นตัวอย่างการดังข้อมูลจาก MySQL มาแสดงบนไฟล์เอกสาร Word ให้อยู่ในรูปแบบตาราง 
 
 
 
เพิ่มเติม 
หรือจะดาวน์โหลดตัวอย่างของ Library 
 
  
 
ซึ่งจะมี Examples อยู่หลายตัว เช่น การแทรกรูปภาพ Images การสร้าง Header และ Footer รวมทั้งการสร้าง Object หรืออื่น ๆ   
 
 
 
Download Code !!           
 
บทความอื่น ๆ ที่เกี่ยวข้อง 
Go to : ภาษาไทยบน PHPWord บทความและวิธีการแก้ปัญหาภาษาไทย (Thai) บน Library ของ PHPWord 
Go to : PHP Word (Word.Application) : การใช้งาน PHP กับ Word : การเรียกใช้งาน Microsoft Word 
Go to : PHP Word header("Content-type: application/vnd.ms-word"); 
Go to : แก้ปัญหา PHPWord แสดงตัวอักษรภาษาไทยไม่ได้ ยังไงดีครับ ใครมีวิธีแก้ช่วยแนะนำหน่อยครับ คือลองใช้ iconv() แล้วแต่มันไม่ได้ครับ 
Go to : PHPWord ภาษาไทย มีปัญหาเรื่องการแสดงภาษาไทย ใครเจออย่างผมบ้าง      
		          
                 
    
                             					
			
              			             
                           
                             |   | 
                               | 
                             	
		                        | 
                            
                           
                             | 
 | 
                               | 
                              
                             			
 | 
           
         
           
          
            
              
                  
                     | 
                   
                  
                    |   | 
                    By :  | 
                    
                      
                      TC Admin	
                     | 
                   
                  
                    |   | 
                    Article :  | 
                    บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ  | 
                   
				  
                  
                    |   | 
                    Score Rating :  | 
                    
                                               | 
                   
                  
                    |   | 
                    Create Date :  | 
                    2012-05-12 | 
                   
				  
                  
                    |   | 
                    Download :  | 
                    
                       No files                     | 
                   
                | 
             
           
		
		
        
        
       |