php 讀資料庫批量生產word文檔

來源:互聯網
上載者:User

 

未使用com組件 使用一個mht的模板 替換其中的titles contents 為資料庫中文章的標題 內容 關鍵詞 

產生word ,本地使用word沒有問題, 但是實際上還是mht的編碼,如果想上傳百度文庫 還需要轉換一步 下面一篇將說明

<?php<br />include("conn.php");<br />function encode($text)<br />{<br />$ucode="";<br />preg_match_all("/[/x80-/xff]?./",$text,$ar);<br />foreach($ar[0] as $v)<br />if(!eregi("[^/x80-/xff]","$v"))<br />$ucode.="&#".utf8_unicode(iconv("GB2312","UTF-8",$v)).";";<br />else<br />$ucode.=$v;<br />return $ucode;<br />}<br />// utf8 -> unicode<br />function utf8_unicode($c) {<br /> switch(strlen($c)) {<br /> case 1:<br /> return ord($c);<br /> case 2:<br /> $n = (ord($c[0]) & 0x3f) << 6;<br /> $n += ord($c[1]) & 0x3f;<br /> return $n;<br /> case 3:<br /> $n = (ord($c[0]) & 0x1f) << 12;<br /> $n += (ord($c[1]) & 0x3f) << 6;<br /> $n += ord($c[2]) & 0x3f;<br /> return $n;<br /> case 4:<br /> $n = (ord($c[0]) & 0x0f) << 18;<br /> $n += (ord($c[1]) & 0x3f) << 12;<br /> $n += (ord($c[2]) & 0x3f) << 6;<br /> $n += ord($c[3]) & 0x3f;<br /> return $n;<br /> }<br />}<br />class word<br />{<br /> function start()<br /> {<br /> ob_start();<br /> // print'<html xmlns:o="urn:schemas-microsoft-com:office:office"<br /> // xmlns:w="urn:schemas-microsoft-com:office:word"<br /> // xmlns="http://www.w3.org/TR/REC-html40">';<br /> }<br /> function save($path)<br /> {<br /> // print "</html>";<br /> $data = ob_get_contents(); </p><p> ob_end_clean();<br /> $this->wirtefile ($path,$data);<br /> }<br /> function wirtefile ($fn,$data)<br /> {<br /> $fp=fopen($fn,"wb") or die("無法開啟");<br /> fwrite($fp,$data) or die("無法寫入");<br /> fclose($fp);<br /> }<br />}<br />/*-------word class End-------*/<br /> $result=mysql_query("SELECT A.title as title,A.aid as aid,A.fid as fid,A.keywords as keywords,B.content as content<br />FROM qb_article A,qb_reply B<br />WHERE A.aid=B.aid and A.aid<50;");<br />$cnt=0;<br />while ($row = mysql_fetch_array($result)) {<br />if(file_exists($row['title'].".doc"))<br />unlink($row['title'].".doc");<br />else<br />{<br />$word=new word;<br />$word->start();<br />//echo "以下是word產生的內容";<br />$contents=@file_get_contents("./wordtemplate.mht");<br />$contents=str_replace("titles",encode($row['title']),$contents);<br />$contents=str_replace("@contents","<mce:style><!--<br /> p{ line-height:30px; font-size:20px;}<br />--></mce:style><style mce_bogus="1"> p{ line-height:30px; font-size:20px;}</style><br />".encode($row['content']),$contents);<br />$contents=str_replace("keywords",encode($row['keywords']),$contents);<br />echo $contents;<br />$wordname="./word/".$row['title'].".doc";<br />$word->save($wordname);//儲存word並且結束.<br />$cnt++;<br />}<br />}</p><p>?><br /><div align="center"><a href="<?php echo $wordname ; ?>" target=_blank class="unnamed1">產生<?=$cnt?>個</a> </div><br /> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.