数据输出保存生成word文档

            ob_start(); //打开缓冲区  
            $header_str = '<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><body>';  
            $footer_str = '</body></html>'; 
            if(!$expTableData){
                echo $header_str.'<h2 align="left">没有匹配到相关数据!</h2>'.$footer_str;
            }else{
                $body_str = '';
                $old_name = '';
                foreach ($expTableData as $key => $value) {    
                    if($value['p_img'] == ','){                        
                        continue;
                    }    
                    $name_tmp = explode(',',$value['n_img']); 
                    $path_tmp = explode(',',$value['p_img']); 
                    if($old_name != $value['real_name']){
                        $body_str .= '<h4>'.$value['real_name'].'</h4>';
                    }
                    $old_name = $value['real_name'];
                    $body_str .= '
                    <table>           
                    <tr><th colspan="2" align="left">'.$value['pt_title'].'</th></tr>            
                    <tr><td><img class="media-object" alt="'.array_shift($name_tmp).'" src="'.array_shift($path_tmp).'" width="300" height="200" onerror="javascript:this.src=\'/Public/home/images/timg.jpg\'"></td>';
                    $tmp = array_shift($path_tmp);
                    if($tmp){
                        $body_str .= '<td><img class="media-object" alt="'.array_shift($name_tmp).'" src="'.$tmp.'" width="300" height="200" onerror="javascript:this.src=\'/Public/home/images/timg.jpg\'"></td></tr></table>';
                    }else{
                        $body_str .= '<td>&nbsp;</td></tr></table>';
                    }
                    
                }
                echo $header_str.$body_str.$footer_str;
            }
        Header("Cache-Control: public");  
        Header("Content-type: application/octet-stream");  
        Header("Accept-Ranges: bytes");  
        $file_name = "人员信息".date("YmdHis").".doc";
        if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) {  
        header('Content-Disposition: attachment; filename='.$file_name);  
        }else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) {  
        Header('Content-Disposition: attachment; filename='.$file_name);  
        } else {  
        header('Content-Disposition: attachment; filename='.$file_name);  
        }  
        header("Pragma:no-cache");  
        header("Expires:0");  
        ob_end_flush();//输出全部内容到浏览器 

 

posted @ 2019-01-04 13:41  赤脚大仙~  阅读(319)  评论(0编辑  收藏  举报
View Code