用mht格式生成的word文档不适合批量导出,用纯HTML生成的word文件可以批量导出。我不适用框架直接用localhost本地执行PHP文件的格式如下:
<?php
header("content-type:text/html;charset=utf-8");
class word{function start(){ob_start();echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:w="urn:schemas-microsoft-com:office:word"xmlns="http://www.w3.org/TR/REC-html40">';}function save($path){ echo "</html>";$data = ob_get_contents();ob_end_clean(); $this->wirtefile ($path,$data);}function wirtefile ($fn,$data){$fp=fopen($fn,"wb");fwrite($fp,$data);fclose($fp);}
}
$basic_code = '001';
$sce_name = '大明湖';
$sce_status = '已开发';
$area_name = '山东省济南市历下区';
$sce_addr = '山东省济南市历下区';
$sce_coord = '117.03246,36.680728';
$sce_form = '两面荷花三面柳,半城山色半城湖';
$now_status = '收费+免费';
$html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<xml><w:WordDocument><w:View>Print</w:View></xml>
</head>';
$html .= '<body>
<h1 style="text-align:center">旅游资源普查表</h1>
<div>
<span>代码:'.$basic_code.'</span>
<span>序号:</span>
</div>
<table border="1" cellpadding="3" cellspacing="0">
<tr><td width="100">名 称:</td><td width="700" colspan="12">'.$sce_name.'</td>
</tr>
<tr><td width="100">开发状况:</td><td width="700" colspan="12">'.$sce_status.'</td>
</tr>
<tr><td width="100">所属管辖:</td><td width="700" colspan="12">'.$area_name.'</td>
</tr>
<tr><td width="100">位 置:</td><td width="700" colspan="12">'.$sce_addr.'</td>
</tr>
<tr><td width="100">地理坐标:</td><td width="700" colspan="12">'.$sce_coord.'</td>
</tr>
<tr><td width="100">基本特征:</td><td width="700" colspan="12"> '.$sce_form.'</td>
</tr>
<tr><td width="100">保护与开发现状:</td><td width="700" colspan="12">'.$now_status.'</td>
</tr>';
// foreach($img_src as $v){// $html .= '<tr>// <td width="100" valign="center" >图片显示</td>// <td width="700" valign="center" colspan="12" ><img src="'.$v.'" width="400" /></td>// </tr>';
// }
$html .= '</table></body>';
//批量生成(自己修改)
for($i=1;$i<=3;$i++){ $word = new word(); $word->start(); //$html = "aaa".$i; $wordname = 'test'.$i.".doc"; echo $html; $word->save($wordname); ob_flush();//每次执行前刷新缓存 flush();
}
注意:该php文件只在本地(服务器)生成word文档,没有写下载功能。如有需要请参考《生成mht格式导出word》那篇文档。