PHP WORLD的正确操作姿势
PHP导出WORLD
1、下载最新PHPWORD 类库 http://phpword.codeplex.com/
<?phpfunction outword(){ vendor('PHPWord.PHPWord'); vendor('PHPWord.PHPWord.Shared.Font'); $PHPWord = new \PHPWord(); $section = $PHPWord->createSection();//添加默认页 //定义table样式 $styleTable = array('borderSize'=>6,'borderColor'=>'006699','cellMargin'=>80);//第一行样式 $styleFirstRow = array('borderBottomSize'=>18,'borderBottomColor'=>'0000FF','bgColor'=>'66BBFF');//定义列样式 $styleCell = array('valign'=>'center'); //使用addCell的第二个参数来给单元格设置样式 //$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR); //为第一行定义字体样式 $fontStyle = array('bold'=>true, 'align'=>'center'); //添加table样式 $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow); //添加表格table $table = $section->addTable('myOwnTableStyle'); $PHPWord->setDefaultFontName('仿宋'); // 全局字体 $PHPWord->setDefaultFontSize(16); // 全局字号为3号 //添加行的高度 $table->addRow(900); //添加列 $table->addCell(1000, $styleCell)->addText('品牌id', $fontStyle); $table->addCell(2000, $styleCell)->addText('品牌名称', $fontStyle); $table->addCell(1000, $styleCell)->addText('父类', $fontStyle); $table->addCell(1000, $styleCell)->addText('状态', $fontStyle); $table->addCell(2000, $styleCell)->addText('添加时间', $fontStyle); $table->addCell(4000, $styleCell)->addText('图片', $fontStyle); //准备数据 $brand = M('Brand'); $where = "status = '1'"; $list = $brand->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select(); //dump($list);exit; //遍历数据 foreach ($list as $k=>$v){ $table->addRow(); $table->addCell(2000)->addText($v['id']); $table->addCell(2000)->addText($v['name']); $table->addCell(2000)->addText($v['pid']); $table->addCell(2000)->addText($v['status']); $table->addCell(2000)->addText($v['addtime']); $table->addCell(2000)->addText($v['logo']); } $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('AdvancedTable.docx'); $fileName = "word报表".date("YmdHis"); header("Content-type: application/vnd.ms-word"); header("Content-Disposition:attachment;filename=".$fileName.".docx"); header('Cache-Control: max-age=0'); $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('php://output'); }
注意:
如果中文出现乱码后,我们就做以下操作吧,简单的是直接添加转化代码,但多了就会很臃肿,治标不治本呀!
$title = iconv('utf-8','GB2312//IGNORE','我要中文不乱码');
2.情况一
a) /Writer/Word2007/Base.php文件内容349行,添加 $objWriter->writeAttribute('w:eastAsia', $font); // 添加这行
b) Section.php中找到代码$givenText = utf8_encode($text);,删除或者注释掉这行代码,添加$givenText = iconv('gbk', 'utf-8', $text);
c) 同时编辑PHPWord/Template.php,找到代码$replace = utf8_encode($replace);,删除或者注释掉这行代码,添加$replace = iconv( 'gbk','utf-8', $replace);
d) $section->addText(iconv('utf-8','GB2312//IGNORE',$st));
3.解决注册不了,与框架冲突(new 不到对象)
public static function Register() { return spl_autoload_register(array('PHPWord_Autoloader', 'Load')); }
修改为
public static function Register() { $functions = spl_autoload_functions(); if($functions){ foreach ($functions as $v){ spl_autoload_unregister($v); } $functions = array_merge(array(array('PHPWord_Autoloader', 'Load')),$functions); foreach ($functions as $v){ spl_autoload_register($v); } }else{ spl_autoload_register(array('PHPWord_Autoloader', 'Load')); } }
也可以关注公众号,里面也有推些新的内容!
欢迎吐槽交流。