PHP 生成doc word文档导出

  public function doc_testOp()
    {
        //$data   = $this->db->where(array('id'=>1))->find();//从数据库查询一条数据
        //$this->assign($data);//把获取的数据传递的模板,替换模板里面的变量
        //$content = $this->fetch('word');//获取模板内容信息word是模板的名称
        $content = '


报名表测试




报名表测试

编号:{$number}

姓名 {$username} 报考专业 {$Zhiyuan[$zyid][\'name\']}
性别 {:Sex($sex)} 出生年月 {$birthday} 民族 {$ethnic}
身高 {$height} 体重 {$weight} 视力 {$vision}
手机 {$mobile} 固定电话 {$tel}
家庭住址 {$address}
毕业学校 小学:{$xschool}
中学:{$zschool}
父母或监护人情况 姓  名 籍贯 身高 工作单位 学历 电  话
{$father[0]} {$father[5]} {$father[1]} {$father[3]} {$father[2]} {$father[4]}
{$mother[0]} {$mother[5]} {$mother[1]} {$mother[3]} {$mother[2]} {$mother[4]}
父母或监护人情况 姓  名 籍贯 身高 工作单位 学历 电  话
{$father[0]} {$father[5]} {$father[1]} {$father[3]} {$father[2]} {$father[4]}
{$mother[0]} {$mother[5]} {$mother[1]} {$mother[3]} {$mother[2]} {$mother[4]}
父母或监护人情况 姓  名 籍贯 身高 工作单位 学历 电  话
{$father[0]} {$father[5]} {$father[1]} {$father[3]} {$father[2]} {$father[4]}
{$mother[0]} {$mother[5]} {$mother[1]} {$mother[3]} {$mother[2]} {$mother[4]}
学习培训简历 {$training}
专项 成绩 获奖 情况 {$training}
'; $fileContent =$this->WordMake($content);//生成word内容 $name = iconv("utf-8", "GBK",'测试文档');//转换好生成的word文件名编码 //$fp = fopen($name.".doc", 'w');//打开生成的文档 //fwrite($fp, $fileContent);//写入包保存文件 //header($fp); @header("Content-Type: application/doc"); @header("Content-Disposition: attachment; filename=" . $name . ".doc"); echo stripslashes($fileContent); } /** * 根据HTML代码获取word文档内容 * 创建一个本质为mht的文档,该函数会分析文件内容并从远程下载页面中的图片资源 * 该函数依赖于类WordMake * 该函数会分析img标签,提取src的属性值。但是,src的属性值必须被引号包围,否则不能提取 * * @param string $content HTML内容 * @param string $absolutePath 网页的绝对路径。如果HTML内容里的图片路径为相对路径,那么就需要填写这个参数,来让该函数自动填补成绝对路径。这个参数最后需要以/结束 * @param bool $isEraseLink 是否去掉HTML内容中的链接 */ function WordMake( $content , $absolutePath = "" , $isEraseLink = true ) { //import("Wordmaker.class"); require_once BASE_DATA_PATH.'/vendors/doc/Wordmaker.class.php'; $mht = new Wordmaker(); if ($isEraseLink){ $content = preg_replace('/(\s*.*?\s*)<\/a>/i' , '$1' , $content); //去掉链接 } $images = array(); $files = array(); $matches = array(); //这个算法要求src后的属性值必须使用引号括起来 if ( preg_match_all('//i',$content ,$matches ) ){ $arrPath = $matches[1]; for ( $i=0;$i<count($arrPath);$i++) { $path = $arrPath[$i]; $imgPath = trim( $path ); if ( $imgPath != "" ) { $files[] = $imgPath; if( substr($imgPath,0,7) == 'http://') { //绝对链接,不加前缀 } else { $imgPath = $absolutePath.$imgPath; } $images[] = $imgPath; } } } $mht->AddContents("tmp.html",$mht->GetMimeType("tmp.html"),$content); for ( $i=0;$i<count($images);$i++) { $image = $images[$i]; if ( @fopen($image , 'r') ) { $imgcontent = @file_get_contents( $image ); if ( $content ) $mht->AddContents($files[$i],$mht->GetMimeType($image),$imgcontent); } else { echo "file:".$image." not exist!
"; } } return $mht->GetFile(); }

效果图就是这样的:

PHP 生成doc word文档导出_第1张图片

只是测试一下 - - 在网上看到的,上面是我自己随便写的测试代码 ,需要优化的。

原文地址在这里:

https://www.moretouch.com.cn/news/jishufenxiang/31.html

需要的可以自己好好研究下。

转载于:https://www.cnblogs.com/lixiaoquan/p/8557127.html

你可能感兴趣的:(php,数据库)