【发布时间】:2016-01-19 22:45:03
【问题描述】:
我在我的 Codeigniter 项目中使用 PHPWord 作为库。在我的项目中,我想生成一个 .docx 文件,该文件使用已经在工作的模板。该模板中有一个贴纸,将通过查询数据库中的数据来填充。由于我想在一个文档中生成多个贴纸,我必须复制我已经使用的第一个模板并添加分页符。但它不起作用。我无法打开文档。它说,“内容有问题”。
这是我的代码:
public function Export(){
$this->load->library('PHPWord');
$this->load->model('person_model');
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$persons= $this->person_model->DataSticker();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$counter=1;
foreach($persons as $row){
$this->Doc($document,$counter,$row);
$counter+=1;
if($counter>6){
$counter=1;
$section->addObject($document);
$section->addPageBreak();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$this->Doc($document,$counter,$row);
}
}
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$filename = 'nameOfFile.docx';
$objWriter->save( $filename);;
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
unlink($filename);
}
public function Doc($document,$counter,$row){
$document->setValue('Fname'.$counter, $row->fname);
$document->setValue('Mname'.$counter, $row->mname);
$document->setValue('Lname'.$counter, $row->lname);
$document->setValue('DOB'.$counter, $row->dob);
$document->setValue('POB'.$counter,$row->pob);
$document->setValue('Age'.$counter, $row->age);
$document->setValue('School'.$counter, $row->school);
}
我们将非常感谢您的帮助。
【问题讨论】:
-
试试这个
\$section->addPageBreak(); -
@Abdulla,顺便说一句,
$section->addObject($document);正确吗?
标签: codeigniter templates phpword