最近在开发有关考试试卷相关的内容,需要生成试卷的word文档并下载。
这里用到了phpoffice/phpword插件,这里记录下一些开发过程中注意的点。
1.安装phpoffice/phpword
composer require phpoffice/phpword
2.准备一个word模板方便向其中写入需要生成的内容


3.编写生成word的方法
<?php
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\TemplateProcessor;
class PaperLogic extends Logic
{
public function downloadPaper(array $papdfInfo)
{
$baseData = [];
$templatePath = resource_path('template/paper/试卷模板.docx');
$baseData['paper_name'] = $paperInfo['name'];
$baseData['paper_topic_num'] = $paperInfo['topic_num'];
$baseData['paper_num'] = $paperInfo['num'];
$baseData['paper_score'] = $paperInfo['score'];
$templateProcessor = new TemplateProcessor($templatePath);
$templateProcessor->setValues($baseData);
$topic_list = $paperInfo['topic_list'];
foreach ($topic_list as $key => $val) {
$question_list = $val['question_list']->toArray();
foreach ($question_list as $k => $v) {
$option_arr = [];
if (in_array($v['type'], ['1', '2', '3'])) {
foreach ($v['option'] as $d) {
$option_arr[] = $d['key'] . ':' . $d['label'];
}
}
$v['option_arr'] = $option_arr;
$v['answer'] = implode('、', $v['answer']);
$question_list[$k] = $v;
}
$val['question_list'] = $question_list;
$topic_list[$key] = $val;
}
$table = new Table([
'borderSize' => 10,
'borderColor' => 'FFFFFF',
'unit' => 'nil',
]);
foreach ($topic_list as $k => $v) {
$table->addRow();
if ($v['describe']) {
$table->addCell(10870, ['valign' => 'center'])->addText($v['name'] . '(' . $v['describe'] . ')', null, ['align' => 'left']);
} else {
$table->addCell(10870, ['valign' => 'center'])->addText($v['name'], null, ['align' => 'left']);
}
foreach ($v['question_list'] as $b) {
$table->addRow();
$table->addCell(10870, ['valign' => 'center'])->addText($b['index'] . '.' . $b['name'] . ' 【' . $b['type_text'] . '】 ' . '(' . $b['score'] . '分)', null, ['align' => 'left']);
$table->addRow();
if (in_array($b['type'], ['1', '2'])) {
$b['option_str'] = implode('<w:br/>', $b['option_arr']);
$table->addCell(10870, ['valign' => 'center'])->addText($b['option_str'], null, ['align' => 'left']);
$table->addRow();
$table->addCell(10870, ['valign' => 'center'])->addText("正确选项:" . $b['answer_key'] . "<w:br/>正确答案:" . $b['answer'] . "<w:br/>答案解析:" . $b['analysis'], null, ['align' => 'left']);
} else if ($b['type'] == '3') {
$b['option_str'] = implode('<w:br/>', $b['option_arr']);
$table->addCell(10870, ['valign' => 'center'])->addText($b['option_str'], null, ['align' => 'left']);
$table->addRow();
$table->addCell(10870, ['valign' => 'center'])->addText("正确答案:" . $b['answer'] . "<w:br/>答案解析:" . $b['analysis'], null, ['align' => 'left']);
} else if ($b['type'] == '4') {
$b['answer'] = str_replace('&', '或', $b['answer']);
$table->addCell(10870, ['valign' => 'center'])->addText("正确答案:" . $b['answer'] . "<w:br/>答案解析:" . $b['analysis'], null, ['align' => 'left']);
} else if ($b['type'] == '5') {
$table->addCell(10870, ['valign' => 'center'])->addText("正确答案:" . $b['answer'] . "<w:br/>答案解析:" . $b['analysis'], null, ['align' => 'left']);
}
}
}
$templateProcessor->setComplexBlock('table', $table);
$tempPath = storage_path("app/public/temp/new_word.docx");
$templateProcessor->saveAs($tempPath);
}
}
4.生成结果
