php使用phpword插件生成word文档

jerry thinkphp 2015年11月19日 收藏
php使用phpword插件生成word文档
php使用phpword插件生成word文档,有需要的朋友可以参考参考。
  1.         <?php 
  2.          
  3.         require_once '../libs/PHPWord/PHPWord.php'; 
  4.         require_once '../libs/PHPWord/PHPWord/IOFactory.php'; 
  5.         require_once '../../config.php'; 
  6.         // require_once '../common/conn.php'; 
  7.          
  8.          
  9.         // New Word Document 
  10.         $PHPWord = new PHPWord(); 
  11.          
  12.         /**********文本格式的word text.php************/ 
  13.         // New portrait section 
  14.          
  15.         //逗号 分割字符串 
  16.         $arr = $_REQUEST['arr']; 
  17.         $a = explode(',',$arr); 
  18.         //echo $arr; 
  19.         date_default_timezone_set("Asia/Shanghai");//设置一个时区 
  20.         $tm=date('Y-m-d H:i:s'); 
  21.         //exit($tm); 
  22.         /**********前多日雨量*********/ 
  23.         if(in_array('1', $a, TRUE)){ 
  24.         $section = $PHPWord->createSection(); 
  25.         $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false,
  26.          'size'=>16)); 
  27.         $PHPWord->addParagraphStyle('pStyle', array('align'=>'center',
  28.          'spaceAfter'=>100)); 
  29.         $c = "前三日雨量报表"; 
  30.         $section->addText($c, 'rStyle', 'pStyle'); 
  31.          
  32.         $styleTable = array('borderSize'=>6, 'borderColor'=>'006699',
  33.          'cellMargin'=>80); 
  34.         $styleFirstRow = array('borderBottomSize'=>18, 
  35.         'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF'); 
  36.          
  37.         // Define cell style arrays 
  38.         $styleCell = array('valign'=>'center'); 
  39.         // Define font style for first row 
  40.         $fontStyle = array('bold'=>true, 'align'=>'center'); 
  41.         //设置标题 
  42.         $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true,
  43.          'size'=>16)); 
  44.         $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 
  45.         'spaceAfter'=>100)); 
  46.          
  47.         // Add table style 
  48.         $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow); 
  49.          
  50.         // Add table 
  51.         $table = $section->addTable('myOwnTableStyle'); 
  52.          
  53.         // Add row设置行高 
  54.         $table->addRow(500); 
  55.          
  56.         $table->addCell(2300, $styleCell)->addText('站码', $fontStyle); 
  57.         $table->addCell(2300, $styleCell)->addText('站名', $fontStyle); 
  58.         $table->addCell(2300, $styleCell)->addText('雨量', $fontStyle); 
  59.         $table->addCell(2300, $styleCell)->addText('水文站监测类型', $fontStyle); 
  60.          
  61.         $conn = mssql_connect($config['mssql']['host'],$config['mssql']['user'],$config['mssql']['password']); 
  62.         mssql_select_db($config['mssql']['dbname'],$conn); 
  63.          
  64.         $stm = date('Y-m-d H:i:s',strtotime('-3 days')); 
  65.         $sql = "EXEC HNOW05_GETPPSPACE '','','".$stm."',1,1"; 
  66.         $res=mssql_query($sql); 
  67.          
  68.         while($arr = mssql_fetch_array($res)){ 
  69.         //echo $arr["STCD"]." 
  70.         "; 
  71.         $table->addRow(); 
  72.         $table->addCell(2300)->addText($arr["STCD"]); 
  73.         $table->addCell(2300)->addText($arr["STNM"]); 
  74.         $table->addCell(2300)->addText($arr["P"]); 
  75.         if($arr["STTP"] == 'MM'){ 
  76.         $table->addCell(2300)->addText('气象站'); 
  77.         }else if($arr["STTP"] == 'BB'){ 
  78.         $table->addCell(2300)->addText('蒸发站'); 
  79.         }else if($arr["STTP"] == 'DD'){ 
  80.         $table->addCell(2300)->addText('堰闸水文站'); 
  81.         }else if($arr["STTP"] == 'TT'){ 
  82.         $table->addCell(2300)->addText('落潮位站'); 
  83.         }else if($arr["STTP"] == 'DP'){ 
  84.         $table->addCell(2300)->addText('泵站'); 
  85.         }else if($arr["STTP"] == 'SS'){ 
  86.         $table->addCell(2300)->addText('墒情站'); 
  87.         }else if($arr["STTP"] == 'PP'){ 
  88.         $table->addCell(2300)->addText('雨量站'); 
  89.         }else if($arr["STTP"] == 'ZZ'){ 
  90.         $table->addCell(2300)->addText('河道水位水文站'); 
  91.         }else if($arr["STTP"] == 'RR'){ 
  92.         $table->addCell(2300)->addText('水库水文站'); 
  93.         }else if($arr["STTP"] == 'ZG'){ 
  94.         $table->addCell(2300)->addText('地下水站'); 
  95.         }else if($arr["STTP"] == 'ZB'){ 
  96.         $table->addCell(2300)->addText('分洪水位站'); 
  97.         } 
  98.         } 
  99.         $section->addTextBreak(2); 
  100.         }else{ 
  101.          
  102.         } 
  103.          
  104.         /******地质灾害*******/ 
  105.         if(in_array('3', $a, TRUE)){ 
  106.         $section = $PHPWord->createSection(); 
  107.         $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 
  108.         'size'=>16)); 
  109.         $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 
  110.         'spaceAfter'=>100)); 
  111.         $c = "地质灾害"; 
  112.         $section->addText($c, 'rStyle', 'pStyle'); 
  113.          
  114.         $content="根据市气象局未来24小时降雨预报和市水利局实时降雨数据,市国土资源局进行了地质灾害预报,请有关部门关 
  115.          
  116.          
  117.          
  118.         实时预警信息,做好地质灾害防范工作"; 
  119.         $section->addText($content); 
  120.         // Add image elements 
  121.         $section->addImage("images/image001.jpg", array('width'=>600, 
  122.         'height'=>480, 'align'=>'center')); 
  123.         }else{ 
  124.          
  125.         } 
  126.         // Save File 
  127.         $fileName = "word报表".date("YmdHis"); 
  128.         header("Content-type: application/vnd.ms-word"); 
  129.         header("Content-Disposition:attachment;filename=".$fileName.".docx"); 
  130.         header('Cache-Control: max-age=0'); 
  131.         $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); 
  132.         $objWriter->save('php://output'); 
  133.         ?> 
  134.         
转载原文地址:
  1. http://www.jb100.net/html/content-22-572-1.html