1、将PHPWord的包放到Public文件夹下
2、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。
//导出word
public function exportWord(){
set_time_limit(0); //避免文件过大时超时
require_once './Public/PHPWord/PHPWord.php';
require_once './Public/PHPWord/PHPWord/IOFactory.php';
// 创建新文档
$PHPWord = new PHPWord();
// 新节
$section = $PHPWord->createSection();//新建页面
// 页脚
$footer = $section->createFooter();
$footer->addPreserveText(' —{PAGE}— ', array('align'=>'center',size=>"10"));
//添加表格样式
$styleTable=array('borderColor'=>'000000','borderSize'=>6);
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable);
$db=M();
//分类标题页样式
//$fontstyle1=array(name=>'方正小标宋简体',size=>'小初');
// 第一部分第一页-生成封皮
$section->addTextBreak(11);//回车
$section->addText('第一部分',array(size=>'34'),array(align=>'center')); //addText(文本,文本样式-数组,段落样式-数组)添加文本
$section->addText('**市清单',array(size=>'34'),array(align=>'center'));
$section->addPageBreak(); //添加分页符
//循环页面生成内容
$dept=$db->query("select * from dept order by deptid asc");
foreach($dept as $meigedept){
//所有未冻结的事项(大项只要有一项不冻结就算保留事项)
$sql="select id,itemname,childname,sum(case when dongjie<>1 then 1 else 0 end) as zixiang from item where substring(id,1,2)<>'10'
and substring(id,3,2)=".$meigedept['deptid']." group by id";
$thisdept=$db->query($sql);
$count=0;
foreach($thisdept as $a){
if($a['zixiang']>0){
$count+=1;
}
}
if($count>0){
//标题以及合计
$section->addText('**市'.$meigedept['deptname'].'事项汇总表',array(),array(align=>'center'));
$section->addText('(共'.$count.'项)',array(),array(align=>'right'));
//添加表格的表头
//$tablestyle=array('borderColor'=>'#000');
$table=$section->addTable('myOwnTableStyle'); //生成表格
$table->addRow(); //添加一行
$table->addCell(1500)->addText('序号',array(),array(align=>'center')); //添加单元格,注意要先添加行,addCell(宽度,单元格样式)
$table->addCell(2500)->addText('类型',array(),array(align=>'center'));
$table->addCell(6000)->addText('事项名称',array(),array(align=>'center'));
//查出所有分类下的事项,如果分类下没有则不显示
$type=$db->query("select * from itemtype where typeid<>'10'");
foreach($type as $t){
//类别去掉“类”字
$leibie=str_replace("类", "", $t['typename']);
$select1="select id,childid,itemname,childname,sum(case when dongjie<>1 then 1 else 0 end) as zixiang from item where substring(id,1,2)=".$t['typeid']."
and substring(id,3,2)=".$meigedept['deptid']." group by id";
$exselect1=$db->query($select1);
$count10=0;
foreach($exselect1 as $baoliu){
if($baoliu['zixiang']>0){
$count10+=1;
}
}
//如果此类别有事项,循环输出各事项并判断是否有子项,有子项的用①②③类的型号标出
if(count($exselect1)>0){
//输出类别及统计个数
$table->addRow();
$table->addCell(1500,array('cellMerge' => ' restart'))->addText($t['typename']."共".$count10."项",array(bold=>'true'));
$table->addCell(2500,array('cellMerge' => ' continue'));
$table->addCell(6000,array('cellMerge' => ' continue'));
$leichount=1;
//输出每个事项,判断是由有子项
foreach($exselect1 as $mei){
//无子项
$table->addRow();
$table->addCell(1500)->addText($leichount,array(),array(align=>'center'));
$table->addCell(2500)->addText($leibie,array(),array(align=>'center'));
$table->addCell(6000)->addText($mei['itemname']);
$leichount+=1;
}
}
}
$section->addPageBreak();
}
}
代码中涵盖了常用的基本方法,如果还需要更负责的格式或者章节,可以到PHPWord手册中去寻找对应方法,添加到代码中,上述代码主要是导出了一系列的事项,请参考其中的方法生成自己想要的文档内容即可。