PHP:引用Phpword导出数据到word文档

扶醉桌前 提交于 2020-11-21 03:11:35

下载地址https://www.cnwenhui.cn/html/show-597.html(其中有中文使用手册可以下载看看)

1.首先要下载Phpword类库,放在如下图目录下

 

2.调用方法

 1  public function  daochuword()
 2     {
 3         vendor("PHPWord.PHPWord");
 4             // New Word Document
 5             $PHPWord = new \PHPWord();
 6             // New portrait section
 7             $section = $PHPWord->createSection();
 8             $PHPWord->addFontStyle('rStyle', array('bold'=>true,'color'=>'87CEEB','size'=>35));
 9             $PHPWord->addParagraphStyle('pStyle', array('align'=>'center','spacing'=>120));
10         $xlsModel = M('api_aliucheng');
11         $Data  = $xlsModel->Field('id,u_name,u_addres,u_tel,u_card,u_time,u_imgz,u_imgf,u_class,b_one_beizhu,b_shenhe_p,water_kj,water_wz,water_quyu,water_start_time,water_start_peo,water_start_jingli,water_beizhu,water_xingzhi,watch_num,watch_time,watch_peo,moneyjingli_name,moneyjingli_beizhu,jingli_name')->where(array('jingli_state_san'=>1))->select();
12         foreach($Data as $key=>$val){
13             $fontStyle = array('color'=>'000000', 'size'=>15,'align'=>'center');
14             $PHPWord->addFontStyle('myOwnStyle', $fontStyle);
15             $section->addText(iconv('utf-8', 'gb2312','用户名字:'.$val['u_name']), 'myOwnStyle');
16             $section->addTextBreak(1);
17             $section->addText(iconv('utf-8', 'gb2312','家庭地址:'.$val['u_addres']), 'myOwnStyle');
18             $section->addTextBreak(1);
19             $section->addText(iconv('utf-8', 'gb2312','手机号:'.$val['u_tel']), 'myOwnStyle');
20             $section->addTextBreak(1);
21             $section->addText(iconv('utf-8', 'gb2312','身份证号:'.$val['u_card']), 'myOwnStyle');
22             $section->addTextBreak(1);
23             $section->addText(iconv('utf-8', 'gb2312','申报时间:'.$val['u_time']), 'myOwnStyle');
24             $section->addTextBreak(1);
25             $section->addText(iconv('utf-8', 'gb2312','用水性质:'.$val['water_xingzhi']), 'myOwnStyle');
26             $section->addTextBreak(1);
27             $section->addText(iconv('utf-8', 'gb2312','安装类别:'.$val['u_class']), 'myOwnStyle');
28             $section->addTextBreak(1);
29             $imageStyle = array('width'=>100, 'height'=>80, 'align'=>'center');
30             $section->addText(iconv('utf-8', 'gb2312','身份证正面:'), 'myOwnStyle');
31             $section->addMemoryImage('<img src="http://localhost:8087/waters/Public/uploads/anzhuang//20180518054237.jpg">',$imageStyle);
32            // $section->addMemoryImage($img,$imageStyle);
33             $section->addTextBreak(1);
34             $section->addPageBreak();//分页
35         }
36         $xlsTitle = iconv('utf-8', 'gb2312');//文件名称
37         $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
38         header('pragma:public');
39         header('Content-type:application/vnd.ms-word;charset=utf-8;name="'.$xlsTitle.'.doc"');
40         header("Content-Disposition:attachment;filename=$fileName.doc");//attachment新窗口打印inline本窗口打印
41         header( 'Content-Type: image/jpeg');
42         ob_clean();//关键
43         flush();//关键
44         $objWrite = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
45         $objWrite->save('php://output');
46         exit;
47     }

 

3.导出结果

4.可以发现图片并没有被导出来,是因为word只认电脑路径比如:C:\Users\wrl\Pictures\Saved Pictures,所以导出的时候要注意

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!