:::

18. 產出真實word檔

一、 關於phpWord(http://phpword.codeplex.com)

  1. PHPWord和PHPExcel是同一個單位所寫出的PHP物件,可用來產生docx檔。
  2. 需要的環境如下:PHP 5.2.x 以上、ZipArchive、xmllib等PHP衍生函式庫
  3. 若是您的Office是2007以下,那麼可能需要Microsoft Office Compatibility Packhttp://www.microsoft.com/zh-tw/download/details.aspx?id=3)才能開啟之(共37.2MB)。PHPWord無法產生2003的doc檔(因為非公開格式)。

二、 讓phpWord支援中文

  1.  打開打開子目錄phpword/Writer/Word2007/Base.php,在310行之後多加一行:
    if($font != 'Arial') {
      $objWriter->writeAttribute('w:eastAsia', $font);
      $objWriter->startElement('w:rFonts');
  2. 搜尋utf8_encode,將之註解或刪除。(此法僅適用於網站為UTF-8者)。Big5請用$text = iconv('big5','utf-8',$text); 方法處理之。

三、 基本結構

<?php
require_once '../class/PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('通訊錄');
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename=通訊錄.docx');
header('Cache-Control: max-age=0');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
?>


四、 常用功能

  • 以下若有用到長度,其單位為twip,1twip=1/1440英吋,1 cm=0.4 in=576 twip
$PHPWord->setDefaultFontName('標楷體'); //設定預設字型
$PHPWord->setDefaultFontSize(12);     //設定預設字型大小

$sectionStyle = array('orientation' => null,  'marginLeft' => 900); //頁面設定(orientation 的值可以是橫向landscape或直向portrait。設定項目有:orientation、marginTop、marginLeft、marginRight、marginBottom、borderTopSize、borderTopColor、borderLeftSize、borderLeftColor、borderRightSize、borderRightColor、borderBottomSize、borderBottomColor)
$section = $PHPWord->createSection([$sectionStyle]); //建立一個頁面

$fontStyle = array('color'=>'006699', 'size'=>18, 'bold'=>true); //文字樣式設定(可用的文字設定:size、name、bold、italic、superScript、subScript、underline、Color、fgColor)
$paragraphStyle=array('align' => 'both', 'spaceAfter'=>300);  //段落設定(可用設定:align、spaceBefore、spaceAfter、spacing)
$section->addText('內容', [$fontStyle], [$paragraphStyle] );  //新增文字段落

$section->addTextBreak(2); //換行,可指定換幾行
$section->addPageBreak();  //換頁

$listStyle = array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER); //設定有序(TYPE_NUMBER)或無序(TYPE_BULLET_FILLED)清單
$section->addListItem( $text, [$depth], [$fontStyle], [$listStyle], [$paragraphStyle] ); //新增清單項目,$depth為階層,從0開始。

$section->addLink( $linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]); //加入超連結
$section->addImage( $src, [$style] ); //插入圖片(可用設定:width、height、align,單位為px)

$PHPWord->addTitleStyle( $titleCount, [$fontStyle] ); //設定標題樣式 $titleCount 是指標題幾
$section->addTitle( $text, [$depth] ); //新增標題($depth會對應$titleCount)

$styleTable = array('borderColor'=>'006699', 'borderSize'=>6, 'cellMargin'=>50); //表格樣式(可用設定:cellMarginTop、cellMarginLeft、cellMarginRight、cellMarginBottom、cellMargin、bgColor、 borderTopSize、borderTopColor、borderLeftSize、borderLeftColor、borderRightSize、borderRightColor、borderBottomSize、borderBottomColor、borderInsideHSize、borderInsideHColor、borderInsideVSize、borderInsideVColor、borderSize、borderColor)
$styleFirstRow = array('bgColor'=>'66BBFF'); //首行樣式
$PHPWord->addTableStyle('myTable', $styleTable, $styleFirstRow); //建立表格樣式
$table = $section->addTable('myTable');//建立表格

$table->addRow(); //新增一列

$cellStyle =array('textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR, 'bgColor'=>'C0C0C0'); //儲存格樣式(設定項:valign、textDirection、bgColor、borderTopSize、borderTopColor、borderLeftSize、borderLeftColor、borderRightSize、borderRightColor、borderBottomSize、borderBottomColor)
$table->addCell(2000, [$cellStyle])->addText('內容'); //新增一格


 


:::

搜尋

QR Code 區塊

https%3A%2F%2Fwww.tad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D26%26tbdsn%3D813

書籍目錄

展開 | 闔起

線上使用者

16人線上 (5人在瀏覽線上書籍)

會員: 0

訪客: 16

更多…