Thinkphp 整合 PHPWord生成docx文档
PHPOffice/PHPWord地址:https://github.com/PHPOffice/PHPWord
// $filename = iconv('utf-8', 'gb2312', $filename); //保存为中文名字
基本代码:
<?php
// +----------------------------------------------------------------------
// | 律师OA办公系统 - word文件操作
// +----------------------------------------------------------------------
// | Author: IceCry <admin@zhinizhiwo.com> <http://www.icecry.com>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Home\Controller\CommonController;
class WordController extends CommonController{
/**
* 构造函数
* 文件存储目录在uploads/doc/案件编号 目录
*/
public function _initialize(){
parent::_initialize();
vendor('phpword.bootstrap');
}
/**
* 生成word函数
* 文件存储目录在uploads/doc/案件编号 目录
*
* @param string $id 案件ID
* @param <type> $arr 案件所分数组,判断是否为多个分所联合办案
*/
public function createFile($id, $arr){
//获取案件id
$caseInfo = M('case_info')->where(array('ci_id'=>$id))->find();
$now_uid = $this->login['uid'];
//转文本
$caseInfo['content'] = html2text($caseInfo['content'];
// $filename = uniqid();
$path = './Uploads/doc/case/' . $id .'/';
$isPath = is_dir($path) ? 1 : 0;
if(!$isPath) mkdir($path, 0777, true);
// 生成对应表格
$this->createGzjl($id, $caseInfo, $path);
//判断是否需生成部分合同
if(in_array($caseInfo['ci_stateid'], C('STEP_FINAL'))){
$this->createCth($id, $caseInfo, $path);
}
}
/**
* 生成出庭函
* 根据委托单位性质,修改为“担任你院/单位/委办理...”三种情况
*/
public function createCth($id, $res, $path){
$court = M('com_acceptdept')->where(array('cad_id'=>$res['ci_dealdept']))->field('cad_type, cad_name')->find();
//出庭函如果存在小类则使用小类 否则使用大类
$className = $res['cc_name'];
if($res['cc_class_name']){
$className = $res['cc_class_name'];
}
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('./Uploads/doc_tpl/cth.docx');
$templateProcessor->setValue('company', $res['cd_name']);
$templateProcessor->setValue('casenum', $res['ci_number']);
$templateProcessor->setValue('court', $court['cad_name']);
if($court['cad_type'] == 1 || $court['cad_type'] == 2){
$templateProcessor->setValue('title', "院");
}elseif($court['cad_type'] == 4){
$templateProcessor->setValue('title', "委");
}else{
$templateProcessor->setValue('title', "单位");
}
$templateProcessor->setValue('bailee', $res['ci_bailee']);
//出庭函默认为前两个
$spotArr = explode(',', $res['ci_spotlawyer']);
if(count($spotArr) > 2){
$res['ci_spotlawyer'] = $spotArr[0].",".$spotArr[1];
}
$templateProcessor->setValue('lawyer', $res['ci_spotlawyer']);
$templateProcessor->setValue('class', $className);
$templateProcessor->setValue('address', C('COMPANY_ADDR'));
$templateProcessor->setValue('tel', C('COMPANY_TEL'));
// 二〇一六年十一月二十三日
$date = setDateCn(date('Y-m-d'));
$templateProcessor->setValue('date', $date);
$filename = 'cth_' . $id . '.docx';
$templateProcessor->saveAs($path . $filename);
}
/**
* 生成工作记录
*
* @param string $id 案件id
* @param <type> $res 案件信息
* @param string $path 路径
*
* 如果没有工作记录则生成空白页(前端控制)
*/
public function createGzjl($id, $res, $path){
if(in_array(C('LAWYER_RID'), C('DAILY_ISCHECK'))){
$dailys = M('user_daily')->where(array('adduid'=>$this->login['uid'], 'type'=>1, 'caseid'=>$id, 'ckvalidtime'=>array('gt', 0)))->order('ud_id asc')->select();
}else{
$dailys = M('user_daily')->where(array('adduid'=>$this->login['uid'], 'type'=>1, 'caseid'=>$id))->order('ud_id asc')->select();
}
foreach ($dailys as $k => $v) {
if(in_array(C('LAWYER_RID'), C('DAILY_ISCHECK'))){
$total += $v['ckvalidtime']*60;
}else{
$total += ($v['endtime'] - $v['starttime']);
}
}
//工作日程条数
// $nums = count($memos);
$nums = count($dailys);
//获取页数 每页展示5条
$pages = ceil($nums / 5);
//拆分数组,每5个为一组
$arr = array();
for ($j=0; $j < $nums; $j++) {
// echo $j;
$x = floor($j % 5);//0 1 2 3 4 5
$y = floor($j / 5);//0 0 0 0 0 1
// $arr[$y][$x] = $memos[$j];
$arr[$y][$x] = $dailys[$j];
}
// var_dump($arr);die;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$paragraphStyleName = 'pStyle';
$phpWord->addParagraphStyle($paragraphStyleName, array('size' => 16, 'bold' => true, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));
//判断是否需要生成多页
for($j = $pages; $j > 0; $j--){
$section->addText($res['cd_name'], array('size' => 16), $paragraphStyleName);
$section->addText('工作记录', array('size' => 18), $paragraphStyleName);
$section->addTextBreak(1);
$section->addText('委托人(或法律顾问单位): ' . $res['ci_bailee'] . ' 案由(或事由): ' . $res['ci_cause'], array('size' => 11));
$section->addTextBreak(1);
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '000');
$spanTableStyleName = 'Colspan Rowspan';
$phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
$table = $section->addTable($spanTableStyleName);
//生成单元
$this->createUnit($section, $phpWord, $table, $arr[$pages-$j], $total);
$section->addTextBreak(1);
$section->addTextBreak(1);
$section->addText('第 '.($pages - $j + 1).' 页 (共 '.$pages.' 页)', array('size' => 11), $paragraphStyleName);
$section->addTextBreak(1);
$section->addText('注:本工作记录应交委托人或法律顾问单位.', array('size' => 11));
//如果超过5项执行
if($j > 1){
$section->addPageBreak();
}
}
$filename = 'gzjl_' . $id . '.docx';
$phpWord->save($path . $filename);
}
/**
* 生成表格单元
* 案件工作记录
* 修改为user_daily获取数据
*/
public function createUnit($section, $phpWord, $table, $arr, $total){
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center');
$cellRowContinue = array('vMerge' => 'continue');
$cellColSpan = array('gridSpan' => 1, 'valign' => 'center');
$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
$cellVCentered = array('valign' => 'center');
// var_dump($arr[0]);die;
foreach ($arr as $k => $v) {
$table->addRow(500);
$cell1 = $table->addCell(1200, $cellRowSpan);
$textrun1 = $cell1->addTextRun($cellHCentered);
$textrun1->addText('时间');
$table->addCell(2000, $cellRowSpan)->addText(date('Y/m/d H:i',$v['starttime']) . '-' . date('Y/m/d H:i',$v['endtime']));//时间
$table->addCell(500, $cellRowSpan)->addText('工作内容', null, $cellHCentered);
$table->addCell(5500, array('vMerge' => 'restart', 'valign' => 'center'))->addText(html2text($v['content']), null);
$table->addRow(900);
$cell2 = $table->addCell(1200, $cellRowSpan);
$textrun2 = $cell2->addTextRun($cellHCentered);
$textrun2->addText('地点');
$table->addCell(2000, $cellRowSpan)->addText($v['address']);
$table->addCell(500, $cellRowContinue);
$table->addCell(5500, $cellRowContinue);
$table->addRow(500);
$cell3 = $table->addCell(1200, $cellRowSpan);
$textrun3 = $cell3->addTextRun($cellHCentered);
$textrun3->addText('占用时间');
if(in_array(C('LAWYER_RID'), C('DAILY_ISCHECK'))){
$table->addCell(2000, $cellRowSpan)->addText($v['ckvalidtime'] . '分钟');
}else{
$table->addCell(2000, $cellRowSpan)->addText(($v['endtime'] - $v['starttime']) / 60 . '分钟');
}
$table->addCell(500, $cellRowContinue);
$table->addCell(5500, $cellRowContinue);
//本页共用时间
if(in_array(C('LAWYER_RID'), C('DAILY_ISCHECK'))){
$pageTotal += $v['ckvalidtime'];
}else{
$pageTotal += ($v['endtime'] - $v['starttime']);
}
}
//追加统计行
$table->addRow(300);
$table->addCell(4350, array('gridSpan' => 3, 'valign' => 'center'))->addText('本页累计工作时间: ' . ceil($pageTotal/60) .'分钟');
$table->addCell(4350, array('gridSpan' => 1, 'valign' => 'center'))->addText('完成工作时间: ' . ceil($total/60) .'分钟');
}
}