博客列表 >thinkhphp5.0利用PHPWord导出word

thinkhphp5.0利用PHPWord导出word

Moments的博客
Moments的博客原创
2017年10月23日 14:25:532722浏览
  1. 下载PHPWord放到thinkphp->extend下

  2. 具体代码:

            Loader::import('PHPWord.php');

            Loader::import('PHPWord.IOFactory.php');

            Loader::import('PHPWord.Section.php');

            Loader::import('PHPWord.Template.php');

            Loader::import('PHPWord.Writer.Word2007.php');

            $PHPWord = new \PHPWord();

            $list=Db::name('user')->select();

            $section = $PHPWord->createSection();

            $styleTable = array('borderColor'=>'006699','borderSize'=>6,'cellMargin'=>50);  

            $styleFirstRow = array('bgColor'=>'66BBFF'); 

            $PHPWord->addTableStyle('myTable', $styleTable, $styleFirstRow);

            $table = $section->addTable('myTable');

            foreach($list as $key=>$value)

            {

                   $table->addRow(400);

                   $table->addCell(2000)->addText($value['id']); 

                   $table->addCell(2000)->addText($value['emp_no']); 

                   $table->addCell(2000)->addText($value['password']);

                   $table->addCell(2000)->addText($value['name'],iconv('UTF-8', 'GB2312//IGNORE=', $value['name']));

           }

           $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

           header("Content-type: application/vnd.ms-word");

           header("Content-Disposition:attachment;filename=testExport.docx");

           header('Cache-Control: max-age=0');

           $objWriter->save('php://output'); 

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议