Laravel - PHP 將 Textarea 文字內容轉出 Word docx 格式
最近,需要處理學生考試系統希望能將筆試的內容轉出為 word 格式,以利於批改老師能夠使用追蹤修訂功能進行批改。
在這裡做一個紀錄及範例,如何將 Textarea 文字內容
安裝 Library
首先,在Laravel 專案直接安裝 PHPWord Library
composer require phpoffice/phpword
輸出文字為 Word 檔案
先建立好 Controller 之後,直接實作範例如下:
<?php
...
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
'"Hi 這是我的文件,請問你看得到嗎?Learn from yesterday, <w:br/> live for today, <w:br/> hope for tomorrow. '
. '<w:br/>The important thing is not to stop questioning." '
. '<w:br/>(Albert Einstein)'
);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
header("Content-Disposition: attachment; filename=WordTest.docx");
$objWriter->save("php://output");
如何處理 Textarea 文字轉換輸出為 word
目前,前端學生輸入是使用 textarea ,在作答時文字內容會包含換行
Continue Reading