首頁 後端開發 php教程 php 產生 匯出word(可包含圖片)的程式碼

php 產生 匯出word(可包含圖片)的程式碼

Jul 25, 2016 am 09:04 AM

  1. /**************************************************** **** ********************

  2. 課程:Mht File Maker
  3. 版本:1.2 beta
  4. 連結:bbs.it-home. org
  5. 作者: Wudi
  6. 描述:該類別可以製作.mht檔。
  7. ************************ ***************************** ****************************/
  8. 類別 MhtFileMaker{

  9. var $config = array();
  10. var $headers = array();
  11. var $headers_exists = array();
  12. var $files = array();
  13. var $boundary;
  14. var $dir_base;
  15. var $page_first;
  16. function MhtFile($config = array()){

  17. }
  18. function SetHeader($header){

  19. $this->headers[] = $header;
  20. $key = strtolower(substr($header, 0, strpos($header, ':') ));
  21. $this->headers_exists[$key] = TRUE;
  22. }
  23. function SetFrom($ from){

  24. $this->SetHeader("來自:$from");
  25. }
  26. function SetSubject($subject){

  27. $this->SetHeader("主題:$subject");
  28. } p>
  29. function SetDate($date = NULL, $isttimestamp = FALSE){

  30. if ($date == NULL) {
  31. $date = time();
  32. }
  33. if ($istimestamp == TRUE) {
  34. $date = date('D, d M Y H:i:s O', $date);
  35. }
  36. $this->SetHeader("日期: $date");
  37. }
  38. function SetBoundary($boundary = NULL) {

  39. if ($boundary == NULL) {
  40. $this->boundary = ' --' 。 strtoupper(md5(mt_rand())) 。 >

    function SetBaseDir($dir){

  41. $this->dir_base = str_replace("", "/", realpath($dir));
  42. }
  43. function SetFirstPage($filename){

  44. $this->page_first = str_replace("", "/", realpath("{$this->dir_base}/$filename"));
  45. }
  46. function AutoAddFiles(){

  47. if (!isset($this->page_first)) {
  48. exit ('不設定第一頁。');
  49. }
  50. $$ filepath = str_replace ($this->dir_base, '', $this->page_first);
  51. $filepath = 'http://mhtfile' 。 page_first, $filepath, NULL);
  52. $this->AddDir($this->dir_base);
  53. }
  54. function AddDir($dir){

  55. $handle_dir = opendir($dir);
  56. while ($filename = readdir($handle_dir)) {
  57. if (( $filename!='.') && ($filename!='..') && (" $dir/$filename"!=$this->page_first)) {
  58. if (is_dir("$dir/ $filename")) {
  59. $this->AddDir("$dir/$filename") ;
  60. } elseif (is_file("$dir/$filename")) {
  61. $filepath = str_replace ($this->dir_base, '', "$dir/$filename");
  62. $filepath = 'http://mhtfile' 。 closedir($handle_dir);
  63. }
  64. function AddFile($filename, $filepath = NULL, $encoding = NULL){

  65. if ($filepath == NULL) {
  66. $filepath = $檔名;
  67. }
  68. $mimetype = $this->GetMimeType($filename);
  69. $filecont = file_get_contents($filename);
  70. $this->AddContents( $filepath, $mimetype , $filecont, $encoding);
  71. }
  72. function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){

  73. if ( $encoding == NULL) {
  74. $filecont = chunk_split(base64_encode($filecont), 76);
  75. $encoding = 'base64';
  76. }
  77. $this->files[] = array (' filepath' => $filepath,
  78. 'mimetype' => $mimetype,
  79. 'filecont' => $filecont,
  80. 'encoding' => $encoding);
  81. }
  82. function CheckHeaders(){

  83. if (!array_key_exists('date', $this->headers_exists)) {
  84. $this->SetDate(NULL, TRUE) );
  85. }
  86. if ($this->boundary == NULL) {
  87. $this->SetBoundary();
  88. }
  89. }
  90. function CheckFiles() {

  91. if (count($this->files) == 0) {
  92. 回傳FALSE;
  93. } else {
  94. 回傳TRUE;
  95. }
  96. }
  97. function GetFile(){

  98. $this->CheckHeaders();
  99. if (!$this->CheckFiles()) {
  100. exit ('沒有新增檔案。');
  101. }
  102. $contents = implode("rn", $this->headers);
  103. $contents .= "rn";
  104. $contents .= "MIME 版本:1.0rn";
  105. $contents .= "內容類型:多部分/相關;rn";
  106. $contents .= "tboundary="{$this->;邊界}";rn";
  107. $contents .= "ttype="" . $this->files[0]['mimetype'] . ""rn";
  108. $contents .= "X-MimeOLE:由Mht File Maker v1.0 betan 製作";
  109. $contents .= "rn";
  110. $contents .= "這是一條MIME 格式的多部分訊息.rn";
  111. $contents . = " rn";
  112. foreach ($this->files as $file) {
  113. $contents .= "--{$this->boundary}rn";
  114. $contents .= "內容類型:$ file[mimetype]rn";
  115. $contents .= "內容傳輸編碼:$file[encoding]rn";
  116. $contents .= "內容位置:$file[檔案路徑]rn";
  117. $contents .= "rn";
  118. $contents .= $file['filecont'];
  119. $contents .= "rn";
  120. }
  121. $contents .= "--{$ this->boundary}--rn";
  122. return $contents;
  123. }
  124. function MakeFile($filename){

  125. $contents = $this->GetFile( );
  126. $fp = fopen($filename, 'w');
  127. fwrite($fp, $contents);
  128. fclose($fp);
  129. }
  130. function GetMimeType($filename){

  131. $pathinfo = pathinfo($filename);
  132. switch ($pathinfo['extension']) {
  133. case 'htm': $mimetype = 'text/ html'; Break;
  134. case 'html': $mimetype = 'text/html'; Break;
  135. case 'txt': $mimetype = 'text/plain'; Break;
  136. case 'cgi' : $mimetype = 'text/plain'; Break;
  137. case 'php': $mimetype = 'text/plain'; Break;
  138. case 'css': $mimetype = 'text/css'; Break;
  139. case 'jpg': $mimetype = 'image/jpeg'; Break;
  140. case 'jpeg': $mimetype = 'image/jpeg'; Break;
  141. case 'jpe': $mimetype = ' image/jpeg'; Break;
  142. case 'gif': $mimetype = 'image/gif'; Break;
  143. case 'png': $mimetype = 'image/png';中斷;
  144. 預設: $mimetype = 'application/octet-stream'; Break;
  145. }
  146. return $mimetype;
  147. }
  148. }
  149. ?>
複製程式碼

2、匯出word檔exportdoc.php

  1. /**

  2. * 根據html程式碼取得word文檔內容
  3. * 建立一個本質為mht的文檔,該函數會分析文件內容並從遠端下載頁面中的圖片資源
  4. * 此函數依賴類別MhtFileMaker
  5. * 函數會分析img標籤,提取src的屬性值。但是,src的屬性值必須被引號包圍,否則不能提取
  6. *
  7. * @param string $content HTML內容
  8. * @param string $absolutePath 網頁的絕對路徑。如果HTML內容裡的圖片路徑是相對路徑,那麼就需要填入這個參數,讓函數自動填入絕對路徑。這個參數最後需要以/結束
  9. * @param bool $isEraseLink 是否去掉HTML內容中的連結
  10. */
  11. include_once("docclass." );
  12. function getWordDocument( $content , $absolutePath = "" , $isEraseLink = true )
  13. {
  14. $mht = new MhtFileMaker();
  15. if ($isEraseLink))(s*.*?s*)/i' , '$1' , $content); // 去掉連結
  16. $images = array();

  17. $files = array();
  18. $matches = array();
  19. //這個演算法要求src後的屬性值必須用引號括起來
  20. if ( preg_match_all('/php 產生 匯出word(可包含圖片)的程式碼/i',$content ,$matches ) )
  21. {
  22. $arrPath = $matches[1];
  23. for ( $i=0;$i{
  24. $path = $ arrPath[$i];
  25. $imgPath = trim( $path );
  26. if ( $imgPath != "" )
  27. {
  28. $files[] = $imgPath;
  29. if( substr($imgPath,0,7) == 'http://')
  30. {
  31. //絕對鏈接,不加海外
  32. }
  33. 其他
  34. {
  35. $imgPath = $absolutePath.$imgPath;
  36. }
  37. $images[] = $imgPath;
  38. }
  39. }
  40. }
  41. $mht->AddContents("tmp.html",$$ mht->GetMimeType("tmp.html"),$content);
  42. for ( $ i=0;$i{

  43. $image = $images[$i];
  44. if ( @fopen($image , 'r') )
  45. {
  46. $imgcontent = @file_get_contents( $image );
  47. if ($imgcontent = @file_get_contents( $image );
  48. if ($imgcontent = @file_get_contents( $image );
  49. if ($imgcontent = @file_get_contents( $image );
  50. if ($imgcontent = @file_get_contents( $image );
  51. if ($imgcontent = @file_get_contents( $image );
  52. if ($imgcontent = @file_get_contents( $image );
  53. if ($imgcontent = @file_get_contents( $image );
  54. if( $content )
  55. $mht->AddContents($files[$i],$mht->GetMimeType($image) ),$imgcontent);
  56. }
  57. else
  58. {
  59. echocho "文件:".$image."不存在!
    ";
  60. }
  61. }
  62. return $mht->GetFile();

  63. }
  64. $content=implode("",file( "http://bbs.it-home. org/print.php?id=3548"));
$fileContent = getWordDocument($content,".");
$fp = fopen("hugesky_word .doc", 'w');fwrite($fp, $fileContent);fclose($fp);
?>
複製程式碼


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

您如何防止PHP中的SQL注入? (準備的陳述,PDO) 您如何防止PHP中的SQL注入? (準備的陳述,PDO) Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

See all articles