検索
ホームページphp教程php手册php使用phpword插件生成word文档

php使用phpword插件生成word文档
php使用phpword插件生成word文档,有需要的朋友可以参考参考。        

<?php 
        require_once &#39;../libs/PHPWord/PHPWord.php&#39;; 
        require_once &#39;../libs/PHPWord/PHPWord/IOFactory.php&#39;; 
        require_once &#39;../../config.php&#39;; 
        // require_once &#39;../common/conn.php&#39;; 
        // New Word Document 
        $PHPWord = new PHPWord(); 
        /**********文本格式的word text.php************/ 
        // New portrait section 
        //逗号 分割字符串 
        $arr = $_REQUEST[&#39;arr&#39;]; 
        $a = explode(&#39;,&#39;,$arr); 
        //echo $arr; 
        date_default_timezone_set("Asia/Shanghai");//设置一个时区 
        $tm=date(&#39;Y-m-d H:i:s&#39;); 
        //exit($tm); 
        /**********前多日雨量*********/ 
        if(in_array(&#39;1&#39;, $a, TRUE)){ 
        $section = $PHPWord->createSection(); 
        $PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>false, &#39;italic&#39;=>false,
         &#39;size&#39;=>16)); 
        $PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;,
         &#39;spaceAfter&#39;=>100)); 
        $c = "前三日雨量报表"; 
        $section->addText($c, &#39;rStyle&#39;, &#39;pStyle&#39;);  
        $styleTable = array(&#39;borderSize&#39;=>6, &#39;borderColor&#39;=>&#39;006699&#39;,
         &#39;cellMargin&#39;=>80); 
        $styleFirstRow = array(&#39;borderBottomSize&#39;=>18, 
        &#39;borderBottomColor&#39;=>&#39;0000FF&#39;, &#39;bgColor&#39;=>&#39;66BBFF&#39;); 
        // Define cell style arrays 
        $styleCell = array(&#39;valign&#39;=>&#39;center&#39;); 
        // Define font style for first row 
        $fontStyle = array(&#39;bold&#39;=>true, &#39;align&#39;=>&#39;center&#39;); 
        //设置标题 
        $PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>true, &#39;italic&#39;=>true,
         &#39;size&#39;=>16)); 
        $PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;, 
        &#39;spaceAfter&#39;=>100)); 
        // Add table style 
        $PHPWord->addTableStyle(&#39;myOwnTableStyle&#39;, $styleTable, $styleFirstRow);  
        // Add table 
        $table = $section->addTable(&#39;myOwnTableStyle&#39;);  
        // Add row设置行高 
        $table->addRow(500); 
        $table->addCell(2300, $styleCell)->addText(&#39;站码&#39;, $fontStyle); 
        $table->addCell(2300, $styleCell)->addText(&#39;站名&#39;, $fontStyle); 
        $table->addCell(2300, $styleCell)->addText(&#39;雨量&#39;, $fontStyle); 
        $table->addCell(2300, $styleCell)->addText(&#39;水文站监测类型&#39;, $fontStyle);  
        $conn = mssql_connect($config[&#39;mssql&#39;][&#39;host&#39;],$config[&#39;mssql&#39;][&#39;user&#39;],$config[&#39;mssql&#39;][&#39;password&#39;]); 
        mssql_select_db($config[&#39;mssql&#39;][&#39;dbname&#39;],$conn);  
        $stm = date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;-3 days&#39;)); 
        $sql = "EXEC HNOW05_GETPPSPACE &#39;&#39;,&#39;&#39;,&#39;".$stm."&#39;,1,1"; 
        $res=mssql_query($sql);  
        while($arr = mssql_fetch_array($res)){ 
        //echo $arr["STCD"]." 
        "; 
        $table->addRow(); 
        $table->addCell(2300)->addText($arr["STCD"]); 
        $table->addCell(2300)->addText($arr["STNM"]); 
        $table->addCell(2300)->addText($arr["P"]); 
        if($arr["STTP"] == &#39;MM&#39;){ 
        $table->addCell(2300)->addText(&#39;气象站&#39;); 
        }else if($arr["STTP"] == &#39;BB&#39;){ 
        $table->addCell(2300)->addText(&#39;蒸发站&#39;); 
        }else if($arr["STTP"] == &#39;DD&#39;){ 
        $table->addCell(2300)->addText(&#39;堰闸水文站&#39;); 
        }else if($arr["STTP"] == &#39;TT&#39;){ 
        $table->addCell(2300)->addText(&#39;落潮位站&#39;); 
        }else if($arr["STTP"] == &#39;DP&#39;){ 
        $table->addCell(2300)->addText(&#39;泵站&#39;); 
        }else if($arr["STTP"] == &#39;SS&#39;){ 
        $table->addCell(2300)->addText(&#39;墒情站&#39;); 
        }else if($arr["STTP"] == &#39;PP&#39;){ 
        $table->addCell(2300)->addText(&#39;雨量站&#39;); 
        }else if($arr["STTP"] == &#39;ZZ&#39;){ 
        $table->addCell(2300)->addText(&#39;河道水位水文站&#39;); 
        }else if($arr["STTP"] == &#39;RR&#39;){ 
        $table->addCell(2300)->addText(&#39;水库水文站&#39;); 
        }else if($arr["STTP"] == &#39;ZG&#39;){ 
        $table->addCell(2300)->addText(&#39;地下水站&#39;); 
        }else if($arr["STTP"] == &#39;ZB&#39;){ 
        $table->addCell(2300)->addText(&#39;分洪水位站&#39;); 
        } 
        } 
        $section->addTextBreak(2); 
        }else{ 
        } 
        /******地质灾害*******/ 
        if(in_array(&#39;3&#39;, $a, TRUE)){ 
        $section = $PHPWord->createSection(); 
        $PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>false, &#39;italic&#39;=>false, 
        &#39;size&#39;=>16)); 
        $PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;, 
        &#39;spaceAfter&#39;=>100)); 
        $c = "地质灾害"; 
        $section->addText($c, &#39;rStyle&#39;, &#39;pStyle&#39;);  
        $content="根据市气象局未来24小时降雨预报和市水利局实时降雨数据,市国土资源局进行了地质灾害预报,请有关部门关  
        注  
        实时预警信息,做好地质灾害防范工作"; 
        $section->addText($content); 
        // Add image elements 
        $section->addImage("images/image001.jpg", array(&#39;width&#39;=>600, 
        &#39;height&#39;=>480, &#39;align&#39;=>&#39;center&#39;)); 
        }else{ 
        } 
        // Save File 
        $fileName = "word报表".date("YmdHis"); 
        header("Content-type: application/vnd.ms-word"); 
        header("Content-Disposition:attachment;filename=".$fileName.".docx"); 
        header(&#39;Cache-Control: max-age=0&#39;); 
        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, &#39;Word2007&#39;); 
        $objWriter->save(&#39;php://output&#39;); 
?>

上面是我整理给大家的在php中使用phpword插件生成word文档,希望今后会对大家有帮助。

相关文章:

php中大小写转换函数的用法及案例

PHP基于面向对象实现留言本步骤详解

PHP接口隔离原则(ISP)使用案例解析

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません