在网上查看了很多,在自己电脑试过phpword,但是不知道是自己原因还是phpword的问题,phpword一直不能使用报错,然后自己用了比较偷懒的方式。
首先,生成word文档一般word文档都有个样式或者说模板,所以先把word模板另存为HTML页面,这里注意htm选择没有经过筛选的,因为筛选的会出现样式偏差,
第二步,将word中需要动态填充数据的地方定义为变量(变量随便定义但是不能是和页面中的其他字符串有相同的情况)
第三步,看代码
public function exportWord()
{
/***请将数据查询部分更换为自己的查询,数据查询部分条件已去除****/
//获取班级信息
$class = M('class')->where()->find();
if (!$class){
$this->error('班级信息获取失败!');
}
//获取所有班级内学员信息
$user = D("User")->where()->select();
//通用参数设置
$year = date('Y',time());
$month = date('m',time());
$day = date('d',time());
$agree = "同意!";
$agree = $this->unicode_encode($agree,'UTF-8');//审核意见转换
$dir = ‘文件生成目录’;
if (!is_dir($dir)){ //判断目录是否存在
mkdir ($dir,0777,true);
}
$trainingtime = $this->unicode_encode($class['trainingtime'],'UTF-8');//培训时间转换
//获取证书等级
$levels = $class['class_major'];
$level = $this->unicode_encode($levels,'UTF-8');//申报专业及等级转换
$comment = "该学员已参与".$levels."考试,考试合格,可申报".$levels."证书。";
$comment = $this->unicode_encode($comment,'UTF-8');//意见转换
//压缩
$zip = new ZipArchive(); //实例化压缩类
$zipname = $dir.'/word.zip'; //创建压缩文件
$zip->open($zipname,ZipArchive::CREATE); //打开压缩文件
foreach ($user as $v){
$name = $this->unicode_encode($v['name'],'UTF-8');//姓名转换
if ($v['sex'] == 0){
$sex = '女';
}else{
$sex = '男';
}
$sex = $this->unicode_encode($sex,'UTF-8');//性别转换
$nation = $this->unicode_encode($v['nation'],'UTF-8');//民族转换
$birth = $this->unicode_encode($v['birth'],'UTF-8');//生日转换
$idcard = $v['idcard'];
$phone = $v['phone'];
$political = $this->unicode_encode($v['political'],'UTF-8');//政治面貌转换
$worktime = $v['worktime'];
$email = $v['email'];
$school = $this->unicode_encode($v['school'],'UTF-8');//毕业院校转换
$major = $this->unicode_encode($v['major'],'UTF-8');//专业转换
$education = $this->unicode_encode($v['education'],'UTF-8');//学历转换
$workunit = $this->unicode_encode($v['workunit'],'UTF-8');//工作单位转换
$address = $this->unicode_encode($v['address'],'UTF-8');//地址转换
$unit = $this->unicode_encode($v['unit_properties'],'UTF-8');//单位性质转换
$positional = $this->unicode_encode($v['positional'],'UTF-8');//职称转换
$image = 'http://'.$_SERVER['HTTP_HOST'].$v['image'];
//获取模板内容
$fp = file_get_contents('./word/model.html'); //模板文件地址
//替换mht模板中的变量
$fp = str_replace('${name}',$name,$fp); //替换姓名
$fp = str_replace('${sex}',$sex,$fp); //替换性别
$fp = str_replace('${nation}',$nation,$fp); //替换民族
$fp = str_replace('${birth}',$birth,$fp); //替换生日
$fp = str_replace('${idcard}',$idcard,$fp); //替换身份证
$fp = str_replace('${phone}',$phone,$fp); //替换手机
$fp = str_replace('${political}',$political,$fp); //替换政治面貌
$fp = str_replace('${worktime}',$worktime,$fp); //替换工作年限
$fp = str_replace('${email}',$email,$fp); //替换邮箱
$fp = str_replace('${school}',$school,$fp); //替换毕业院校
$fp = str_replace('${major}',$major,$fp); //替换专业
$fp = str_replace('${education}',$education,$fp); //替换学历
$fp = str_replace('${workunit}',$workunit,$fp); //替换工作单位
$fp = str_replace('${address}',$address,$fp); //替换地址
$fp = str_replace('${unit}',$unit,$fp); //替换单位性质
$fp = str_replace('${positional}',$positional,$fp); //替换职称
$fp = str_replace('${trainingtime}',$trainingtime,$fp); //替换培训时间
$fp = str_replace('${level}',$level,$fp); //替换申报专业及等级
$fp = str_replace('${comment}',$comment,$fp); //替换意见
$fp = str_replace('${agree}',$agree,$fp); //替换审核意见
$fp = str_replace('${image}',$image,$fp); //替换图片
$fp = str_replace('${year}',$year,$fp); //替换年
$fp = str_replace('${month}',$month,$fp); //替换月
$fp = str_replace('${day}',$day,$fp); //替换日
$this->start($fp);
$this->saves($dir.'/'.$v['id'].'.doc');
//添加文件到压缩文件内
$zip->addFile($dir.'/'.$v['id'].'.doc',basename($dir.'/'.$v['id'].'.doc'));
}
$zip->close();
header("Location: http://".$_SERVER['HTTP_HOST'].__ROOT__."/admin/".$zipname);
}
//打开缓存区,输出内容
function start($data)
{
ob_start();
echo $data;
}
//获取缓存区内容,并清空缓存区
function saves($path)
{
$data = ob_get_contents();
ob_end_clean();
$this->wirtefile ($path,$data);
}
//将缓存区内容写入文件
function wirtefile ($fn,$data)
{
$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}
//utf-8中文转Unicode,这里需要注意如果不做转换,生成的word文档中的汉字全部为乱码
function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
$str = iconv($encoding, 'UCS-2', $str);
$arrstr = str_split($str, 2);
$unistr = '';
for($i = 0, $len = count($arrstr); $i < $len; $i++) {
$dec = hexdec(bin2hex($arrstr[$i]));
$unistr .= $prefix . $dec . $postfix;
}
return $unistr;
}
终上,生成word文档完成,其中对批量生成word的文件夹做了压缩,最后的header提供的下载也是压缩文件的下载。