PHP导出WORLD1,下载最新的PHPWORD类库http://phpword.codeplex.com/createSection();//添加默认页面//定义表格样式$styleTable=array('borderSize'=>6,'borderColor'=>'006699','cellMargin'=>80);//首行样式$styleFirstRow=array('borderBottomSize'=>18,'borderBottomColor'=>'0000FF','bgColor'=>'66BBFF');//定义列样式$styleCell=array('valign'=>'中心');//使用addCell的第二个参数来设置单元格的样式//$styleCellBTLR=array('valign'=>'center','textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);//定义第一行的字体样式$fontStyle=array('bold'=>true,'align'=>'center');//添加表格样式$PHPWord->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow);//添加表格表格$table=$section->addTable('myOwnTableStyle');$PHPWord->setDefaultFontName('喜欢歌曲');//全局字体$PHPWord->setDefaultFontSize(16);//全局字体大小为3//添加行的高度$table->addRow(900);//添加列$table->addCell(1000,$styleCell)->addText('brandid',$fontStyle);$table->addCell(2000,$styleCell)->addText('品牌名称',$fontStyle);$table->addCell(1000,$styleCell)->addText('parent',$fontStyle);$table->addCell(1000,$styleCell)->addText('status',$fontStyle);$table->addCell(2000,$styleCell)->addText('添加时间',$fontStyle);$table->addCell(4000,$styleCell)->addText('图片',$fontStyle);//准备数据$brand=M('Brand');$where="状态='1'";$list=$brand->where($where)->order('iddesc')->limit($page->firstRow.','.$page->listRows)->select();//转储($列表);退出;//遍历数据foreach($listas$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("内容类型:application/vnd.ms-word");header("Content-Disposition:attachment;filename=".$fileName.".docx");header('钙che-Control:max-age=0');$objWriter=\PHPHWord_IOFactory::createWriter($PHPHWord,'Word2007');$objWriter->save('php://output');}注意:如果中文出现乱码后,我们进行如下操作。简单的就是直接加上转换代码,但是如果太多的话会很臃肿,是治标不治本!$title=iconv('utf-8','GB2312//IGNORE','我要中文无乱码');2.情况1a)/Writer/Word2007/Base.php文件内容349行,添加$objWriter->writeAttribute('w:eastAsia',$font);//添加这一行b)找到代码$givenText=utf8_encode($text);在Section.php中,删除或注释掉这行代码,添加$givenText=iconv('gbk','utf-8',$text);c)同时编辑PHPWord/Template.php,找到代码$replace=utf8_encode($replace);,删除或注释掉这行代码,添加$replace=iconv('gbk','utf-8',$替换);d)$section->addText(iconv('utf-8','GB2312//忽略',$st));冲突(new无法到达对象)publicstaticfunctionRegister(){returnspl_autoload_register(array('PHPWord_Autoloader','Load'));}改成publicstaticfunctionRegister(){$functions=spl_autoload_functions();if($functions){foreach($functionsas$v){spl_autoload_unregister($v);}$functions=array_merge(array(array('PHPWord_Autoloader','Load')),$functions);foreach($functionsas$v){spl_autoload_register($v);}}else{spl_autoload_register(array('PHPWord_Autoloader','Load'));}}大家也可以关注公众号,里面有一些新内容哦!欢迎吐槽交流。
