Việc tích hợp soạn thảo file văn bản Word tích hợp vào CodeIgniter thật đơn giản; bạn có thể làm theo hướng dẫn như sau:
- Bước 1: Download dữ liệu sau:
Download CodeIgniter
Download PHPWord
- Bước 2: Giải nén PHPWord (chỉ giải nén PHPWord.php and PHPWord folder) và đưa vào thư mục application/third_party của CodeIgniter
Chỉ copy 2 thư mục vào
application/third_party của bạn; kết quả như sau:
Thư mục third_party sau khi thêm PHPWord
- Bước 3: Bây giờ bạn tạo 1 thư viện mới trong application/libraries của CodeIgniter. ví dụ đặt tên là Word.php. Mã nguồn như sau:
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPWord.php";
class Word extends PHPWord {
public function __construct() {
parent::__construct();
}
}
|
- Bước 4: Bây giờ chúng ta gọi và sử dụng thư viện Word từ Controller của bạn:
- Gọi/Load thư viện và tạo ra section mới (Bắt buộc)
|
|
$this->load->library('word');
//our docx will have 'lanscape' paper orientation
$section = $this->word->createSection(array('orientation'=>'landscape'));
|
- Thêm một số định dạng và đoạn văn bản cho nó!
|
|
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(1);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(1);
$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$this->word->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');
|
- Thêm một số ảnh! Chú ý không sử dụng base_url or site_url, PHPWord chỉ sử dụng đường dẫn tuyêt đối đến máy chủ của bạn (Có nghĩa rằng bạn không thể thêm ảnh mà không phải từ máy chủ của bạn).
|
|
// Add image elements
$section->addImage(FCPATH.'/image/_mars.jpg');
$section->addTextBreak(1);
$section->addImage(FCPATH.'/image/_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center'));
$section->addTextBreak(1);
$section->addImage(FCPATH.'/image/_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right'));
|
- Tạo bảng table và đưa thêm 1 số định dạng style
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// 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
$this->word->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
// Add table
$table = $section->addTable('myOwnTableStyle');
// Add row
$table->addRow(900);
// Add cells
$table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);
$table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);
// Add more rows / cells
for($i = 1; $i <= 2; $i++) {
$table->addRow();
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$text = ($i % 2 == 0) ? 'X' : '';
$table->addCell(500)->addText($text);
}
|
- Cuối cùng, Đặt tên văn bản với đuôi .docx để người sử dụng lưu trên máy tính cá nhân.
|
|
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');
$objWriter->save('php://output');
|
- Kết quả của ví dụ này như sau (mở với Ms Word 2007 và xem ở tỷ lệ 50%)
Link tham khảo: http://www.ahowto.net/php/creating-ms-word-document-using-codeigniter-and-phpword/