PHP操作doc文档之PHPWord0.6.1

【住管系统】

{根据用户在微信端对模板doc的选择来生成对应文档。例:用户勾选房屋类别为公租房,则需要在对应的表格中生成,不要再把公租房,廉租房两个选项一起生成}
解决办法:利用PHPWord0.6.1PHPWord官方网址下载生成表格,在数据库中取出用户选择的数据:
①拿到用户选择的数据,并通过函数参数的形式传递给download.php。

1、加载PHP文件:

include 'download.php';

2、拿到并且把用户数据传递给download.php中doc类的download函数

$sqlmax="select ID from tp_zhuguan_doorsign";
$count=count(D("zhuguan_doorsign")->query($sqlmax));
for($i=1;$i<=$count;$i++){
$sqls="select SignNumber from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$SignNumber=mysql_result($query,0);
$sqls="select ApplyPerson from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$ApplyPerson=mysql_result($query,0);
$sqls="select ContactPhone from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$ContactPhone=mysql_result($query,0);
$sqls="select RentPerson from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$RentPerson=mysql_result($query,0);
$sqls="select HouseAddress from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$HouseAddress=mysql_result($query,0);
$sqls="select HouseType from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$HouseType=mysql_result($query,0);
$sqls="select RecordType from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$RecordType=mysql_result($query,0);
$sqls="select ReductionCondition from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$ReductionCondition=mysql_result($query,0);
$sqls="select DoorReason from tp_zhuguan_doorsign where ID=".$i;
$query = mysql_query($sqls);
$DoorReason=mysql_result($query,0);
//调用生成对应表格的函数,并把用户选择的参数传递过去
$doc=new doc();
$doc->download($SignNumber,$ApplyPerson,$ContactPhone,
$RentPerson,$HouseAddress,$HouseType,$RecordType,$ReductionCondition,$DoorReason);
}

②在所加载的download.php文件中生成文档。(PS:文档默认生成在根目录下)

1、加载解压到根目录下的PHPWord
Ⅰ、需要PHPWord文件夹中的PHPWord.php文件
PHP操作doc文档之PHPWord0.6.1_第1张图片
Ⅱ、需要PHPWord文件夹下的PHPWord文件夹中的IOFactory.php文件
PHP操作doc文档之PHPWord0.6.1_第2张图片
2、在函数中加载并生成Word文档

php
class doc{
public function download($SignNumber,$ApplyPerson,$ContactPhone,
$RentPerson,$HouseAddress,$HouseType,$RecordType,$ReductionCondition,$DoorReason){
    require_once 'c:/wamp/www/zf/PHPWord/PHPWord.php';
    require_once 'c:/wamp/www/zf/PHPWord/PHPWord/IOFactory.php';
    $PHPWord = new PHPWord();
    $section = $PHPWord->createSection();
    $PHPWord->setDefaultFontSize(16);
    $section->addText(iconv('utf-8','gbk','成都市公共住房管理中心上门服务预约单'));
    $styleTable = array(  
        'borderSize'=>6,  
        'borderColor'=>'006699',  
        'cellMargin'=>80  
        );  
    $styleFirstRow = array(  
        'borderBottomSize'=>18,  
        'borderBottomColor'=>'0000ff',  
        'bgColor'=>'66bbff'  
        );  
    $styleCell = array('valign'=>'center');  
    $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');    
    $table->addRow(900);  
    $table->addCell(2000,$styleCell)->addText('申请人',$fontStyle);  
    $table->addCell(2400,$styleCell)->addText($ApplyPerson,$fontStyle); 
    $table->addCell(2000,$styleCell)->addText('承租人',$fontStyle);  
    $table->addCell(2400,$styleCell)->addText($RentPerson,$fontStyle);  
    $table->addRow();  
    $table->addCell(2000)->addText("房屋地址");  
    $table->addCell(6800)->addText($HouseAddress);  
    $table->addRow();
    $table->addCell(2000)->addText("房屋类别");
    $table->addCell(2400)->addText($HouseType);  
    $table->addCell(2000)->addText("联系电话");
    $table->addCell(2400)->addText($ContactPhone);
    $table->addRow();
    $table->addCell(2000)->addText("登记类别");
    $table->addCell(6800)->addText($RecordType);
    $table->addRow();
    $table->addCell(2000)->addText("减免条件    (廉租房必须需勾选)");
    $table->addCell(6800)->addText($ReductionCondition);
    $table->addRow();
    $table->addCell(2000)->addText("上门理由");
    $table->addCell(6800)->addText($DoorReason);
    $table->addRow();
    $table->addCell(2000)->addText("预约上门地址");
    $table->addCell(6800)->addText($HouseAddress);
    $table->addRow();
    $table->addCell(2000)->addText("受理上门服务须知");
    $table->addCell(6800)->addText("1、上门服务对象为老、病、残、行动不便的房屋承租人;2、窗口工作人员在受理上门服务时请按固定格式填写“上门服务预约通知单”,要求填写申请人姓名、地址、联系电话、申请上门服务原因、受理人等全部信息;3、窗口工作人员受理上门服务后出示告知单转交窗口周值日,周值日负责统计并转交窗口主任,由窗口主任负责安排上门服务;上门服务咨询电话:(028)86279442。");
    $table->addRow();
    $table->addCell(2000)->addText("受理人");
    $table->addCell(2400)->addText("陈之");
    $table->addCell(2000)->addText("受理时间");
    $table->addCell(2400)->addText("2016.5.16");
    $table->addRow();
    $table->addCell(2000)->addText("经办人");
    $table->addCell(2400)->addText("");
    $table->addCell(2000)->addText("经办日期");
    $table->addCell(2400)->addText("");
    $table->addRow();
    $table->addCell(2000)->addText("办理结果");
    $table->addCell(2400)->addText("");
    $table->addCell(2000)->addText("承租人签字确认");
    $table->addCell(2400)->addText("");
    $section->addText(iconv('utf-8','gbk','备注:受理人必须对廉租住户的各项减免原件进行审核'));
    //生成的文档为Word2007
    $objWriter = PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
    $objWriter->save("$SignNumber.doc");
    }
}
?>

你可能感兴趣的:(PHP)