老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

怎么將PHP網頁導出為Word文件

這篇文章給大家分享的是有關怎么將PHP網頁導出為Word文件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

業務包括:企業網站建設、手機網站制作設計商城系統網站開發、網站營銷推廣等服務,并且涵蓋域名注冊虛擬主機、云服務器、等互聯網基礎服務;創新互聯聯建站以互聯網的創新理念,成熟完善的建站體系,開拓進取的精神,專業的解決方案和顧問咨詢服務,幫助客戶在互聯網時代提升形象、把握商機、實現價值,提高企業的核心競爭力。

原理

一般,有2種方法可以導出doc文檔,一種是使用com,并且作為php的一個擴展庫安裝到

實現

通過以上的原理介紹,相信大家應該對實現的過程有個初步的了解了,下面我給出一個導出函數,這個函數可以將HTML代碼導出成一個mht文檔,參數有3個,其中后2個為可選參數

content:要轉換的HTML代碼

absolutePath: 如果HTML代碼中的圖片地址都是相對路徑,那么這個參數就是HTML代碼中缺少的絕對路徑。

isEraseLink:是否去掉HTML代碼中的超鏈接

返回值為mht的文件內容,您可以通過file_put_content將它保存成后綴名為doc的文件

這個函數的主要功能其實就是分析HTML代碼中的所有圖片地址,并且依次下載下來。獲取到了圖片的內容以后,調用MhtFileMaker類,將圖片添加到mht文件中。具體的添加細節,封裝在MhtFileMaker類中了。

PHP代碼

/** 
 * 根據HTML代碼獲取word文檔內容 
 * 該函數依賴于類MhtFileMaker 
 *  
 * @param string $content HTML內容 
 * @param bool $isEraseLink 是否去掉HTML內容中的鏈接 
 */   
function  getWordDocument(  $content  ,  $absolutePath  =  ""  ,  $isEraseLink  = true )  
{  
     $mht  =  new  MhtFileMaker();  
     if  ( $isEraseLink )  
         $content  = preg_replace( '/
  
     $images  =  array ();  
     $files  =  array ();  
     $matches  =  array ();  
     //這個算法要求src后的屬性值必須使用引號括起來   
     if  ( preg_match_all( '/
    {  
         $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;$iAddContents($files[$i],$mht->GetMimeType($image),$imgcontent); 
        } 
        else 
        { 
            echo " file: ".$image."  not exist!
        }  
    }  
      
     return   $mht ->GetFile();  
}

使用方法:

PHP代碼

$fileContent  = getWordDocument( $content , "http://www.yoursite.com/Music/etc/" );  
$fp  =  fopen ( "test.doc" ,  'w' );  
fwrite( $fp ,  $fileContent );  
fclose( $fp );

其中,$content變量應該是HTML源代碼,后面的鏈接應該是能填補HTML代碼中圖片相對路徑的URL地址

注意,在使用這個函數之前,您需要先包含類MhtFileMaker,這個類可以幫助我們生成Mht文檔。

PHP代碼

headers[] =  $header ;  
         $key  =  strtolower ( substr ( $header , 0,  strpos ( $header ,  ':' )));  
         $this ->headers_exists[ $key ] = TRUE;  
    }  
  
     function  SetFrom( $from ){  
         $this ->SetHeader( "From: $from" );  
    }  
  
     function  SetSubject( $subject ){  
         $this ->SetHeader( "Subject: $subject" );  
    }  
  
     function  SetDate( $date  = NULL,  $istimestamp  = FALSE){  
         if  ( $date  == NULL) {  
             $date  = time();  
        }  
         if  ( $istimestamp  == TRUE) {  
             $date  =  date ( 'D, d M Y H:i:s O' ,  $date );  
        }  
         $this ->SetHeader( "Date: $date" );  
    }  
  
     function  SetBoundary( $boundary  = NULL){  
         if  ( $boundary  == NULL) {  
             $this ->boundary =  '--'  .  strtoupper (md5(mt_rand())) .  '_MULTIPART_MIXED' ;  
        }  else  {  
             $this ->boundary =  $boundary ;  
        }  
    }  
  
     function  SetBaseDir( $dir ){  
         $this ->dir_base =  str_replace ( "\\", " / ", realpath($dir)); 
    } 
 
    function SetFirstPage($filename){ 
        $this->page_first = str_replace(" \\ ", " / ", realpath(" { $this ->dir_base}/ $filename ")); 
    } 
 
    function AutoAddFiles(){ 
        if (!isset($this->page_first)) { 
            exit ('Not set the first page.'); 
        } 
        $filepath = str_replace($this->dir_base, '', $this->page_first); 
        $filepath = 'http://mhtfile' . $filepath; 
        $this->AddFile($this->page_first, $filepath, NULL); 
        $this->AddDir($this->dir_base); 
    } 
 
    function AddDir($dir){ 
        $handle_dir = opendir($dir); 
        while ($filename = readdir($handle_dir)) { 
            if (($filename!='.') && ($filename!='..') && (" $dir / $filename "!=$this->page_first)) { 
                if (is_dir(" $dir / $filename ")) { 
                    $this->AddDir(" $dir / $filename "); 
                } elseif (is_file(" $dir / $filename ")) { 
                    $filepath = str_replace($this->dir_base, '', " $dir / $filename "); 
                    $filepath = 'http://mhtfile' . $filepath; 
                    $this->AddFile(" $dir / $filename ", $filepath, NULL); 
                } 
            } 
        } 
        closedir($handle_dir); 
    } 
 
    function AddFile($filename, $filepath = NULL, $encoding = NULL){ 
        if ($filepath == NULL) { 
            $filepath = $filename; 
        } 
        $mimetype = $this->GetMimeType($filename); 
        $filecont = file_get_contents($filename); 
        $this->AddContents($filepath, $mimetype, $filecont, $encoding); 
    } 
 
    function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){ 
        if ($encoding == NULL) { 
            $filecont = chunk_split(base64_encode($filecont), 76); 
            $encoding = 'base64'; 
        } 
        $this->files[] = array('filepath' => $filepath, 
                               'mimetype' => $mimetype, 
                               'filecont' => $filecont, 
                               'encoding' => $encoding); 
    } 
 
    function CheckHeaders(){ 
        if (!array_key_exists('date', $this->headers_exists)) { 
            $this->SetDate(NULL, TRUE); 
        } 
        if ($this->boundary == NULL) { 
            $this->SetBoundary(); 
        } 
    } 
 
    function CheckFiles(){ 
        if (count($this->files) == 0) { 
            return FALSE; 
        } else { 
            return TRUE; 
        } 
    } 
 
    function GetFile(){ 
        $this->CheckHeaders(); 
        if (!$this->CheckFiles()) { 
            exit ('No file was added.'); 
        } 
        $contents = implode(" \r\n ", $this->headers); 
        $contents .= " \r\n "; 
        $contents .= " MIME-Version: 1.0\r\n "; 
        $contents .= " Content-Type: multipart/related;\r\n "; 
        $contents .= " \tboundary=\ "{$this->boundary}\";\r\n" ;  
         $contents  .=  "\ttype=\""  .  $this ->files[0][ 'mimetype' ] .  "\"\r\n" ;  
         $contents  .=  "X-MimeOLE: Produced By Mht File Maker v1.0 beta\r\n" ;  
         $contents  .=  "\r\n" ;  
         $contents  .=  "This is a multi-part message in MIME format.\r\n" ;  
         $contents  .=  "\r\n" ;  
         foreach  ( $this ->files  as   $file ) {  
             $contents  .=  "--{$this->boundary}\r\n" ;  
             $contents  .=  "Content-Type: $file[mimetype]\r\n" ;  
             $contents  .=  "Content-Transfer-Encoding: $file[encoding]\r\n" ;  
             $contents  .=  "Content-Location: $file[filepath]\r\n" ;  
             $contents  .=  "\r\n" ;  
             $contents  .=  $file [ 'filecont' ];  
             $contents  .=  "\r\n" ;  
        }  
         $contents  .=  "--{$this->boundary}--\r\n" ;  
         return   $contents ;  
    }  
  
     function  MakeFile( $filename ){  
         $contents  =  $this ->GetFile();  
         $fp  =  fopen ( $filename ,  'w' );  
        fwrite( $fp ,  $contents );  
        fclose( $fp );  
    }  
  
     function  GetMimeType( $filename ){  
         $pathinfo  =  pathinfo ( $filename );  
         switch  ( $pathinfo [ 'extension' ]) {  
             case   'htm' :  $mimetype  =  'text/html' ;  break ;  
             case   'html' :  $mimetype  =  'text/html' ;  break ;  
             case   'txt' :  $mimetype  =  'text/plain' ;  break ;  
             case   'cgi' :  $mimetype  =  'text/plain' ;  break ;  
             case   'php' :  $mimetype  =  'text/plain' ;  break ;  
             case   'css' :  $mimetype  =  'text/css' ;  break ;  
             case   'jpg' :  $mimetype  =  'image/jpeg' ;  break ;  
             case   'jpeg' :  $mimetype  =  'image/jpeg' ;  break ;  
             case   'jpe' :  $mimetype  =  'image/jpeg' ;  break ;  
             case   'gif' :  $mimetype  =  'image/gif' ;  break ;  
             case   'png' :  $mimetype  =  'image/png' ;  break ;  
             default :  $mimetype  =  'application/octet-stream' ;  break ;  
        }  
         return   $mimetype ;  
    }  
}  
?>

上面我們討論了通過mht文件,來實現PHP導出doc格式的。這種方法可以解決一個難題,就是使導出的doc文件中包含圖片,當然,如果您要包含更多的內容,比如CSS樣式表,只需要用正則表達式分析HTML代碼中的link標簽,提取css樣式文件的地址,然后讀取并編碼成base64,最后加入到mht文件中就可以了。

感謝各位的閱讀!關于“怎么將PHP網頁導出為Word文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!


分享標題:怎么將PHP網頁導出為Word文件
網頁地址:
http://www.xueling.net.cn/article/iejgci.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 亚洲狠狠色丁香婷婷综合 | 毛片在线网站 | 国产精品麻豆aⅴ人妻 | 欧美自拍亚洲综合丝袜 | 蜜芽AⅤ色欲AV浪潮夜夜嗨 | 啊灬啊灬啊灬快灬高潮了听书 | 亚洲精品成人无限看 | 中文字幕日韩精品在线观看 | 国产精品香蕉在线的人 | 亚洲拍宾馆视频播放 | 最近日本中文字幕免费完整 | 激情综合色综合啪啪五月丁香搜索 | 亚洲欧洲精品视频在线观看 | 成人精品一区二区三区免费看 | 久久精品三 | 18禁成人网站免费观看 | 国产欧美一区二区精品性色超碰 | 欧美精品一本久久男人的天堂 | 久久厕所偷拍视频 | seseavlu视频在线 | 91麻豆国产福利精品 | www.欧美视频 | 国产a级一级| 欧美性受xxxx黑人xxxy爽 | 毛片久久久久久久 | 黄色免费入口 | 久久久久亚洲av成人动图 | 国产精品久久久久久久久久久天堂 | av在线播放观看 | 亚洲色无码A片一区二小说 久久九九影视 | 国产一区三区三区 | 99视频精品在线 | 亚洲av片不卡无码影视 | 久久久久久无码AV成人影院 | 制服丝袜国产AV无码 | 久久久一牛精品99久久精品66 | 国产在线乱码一区二区三区 | 妖精视频www网站入口 | 网友自拍区视频精品 | 九久久久| 精品av中文字幕在线毛片 |