首頁 > 後端開發 > php教程 > php使用phpword產生word文件的例子

php使用phpword產生word文件的例子

WBOY
發布: 2016-07-25 09:04:00
原創
1861 人瀏覽過
  1. require_once '../libs/PHPWord/PHPWord.php';

  2. require_once '../libs /PHPWord/PHPWord/IOFactory.php';
  3. require_once '../../config.php';
  4. // require_once '../common/conn.php';
  5. // New Word Document

  6. $PHPWord = new PHPWord();
  7. /**********文字格式的word text.php************/

  8. // New portrait section
  9. //逗號分割字串

  10. $arr = $_REQUEST['arr'];
  11. $a = explode(',',$arr);
  12. //echo $arr;
  13. date_default_timezone_set("Asia/Shanghai");//設定一個時區
  14. $tm=date('Y-m-d H:i:s');
  15. //exit($tm);
  16. / **********前多日雨量*********/
  17. if(in_array('1', $a, TRUE)){
  18. $section = $PHPWord->createSection();
  19. $PHPWord->addFontStyle('rStyle ', array('bold'=>false, 'italic'=>false, 'size'=>16));
  20. $PHPWord->addParagraphStyle('pStyle', array('align'=>'center' , 'spaceAfter'=>100));
  21. $c = "前三日雨量報表";
  22. $section->addText($c, 'rStyle', 'pStyle');
  23. $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);

  24. $styleFirstRow = array('borderBottomSize'=>18, ' borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
  25. // Define cell style arrays

  26. $styleCell = array('valign'=>'center' );
  27. // Define font style for first row
  28. $fontStyle = array('bold'=>true, 'align'=>'center');
  29. //設定標題
  30. $PHPWord ->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
  31. $PHPWord->addParagraphStyle('pStyle', array('align '=>'center', 'spaceAfter'=>100));
  32. // Add table style

  33. $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow) ;
  34. // Add table

  35. $table = $section->addTable('myOwnTableStyle');
  36. // Add row設定行高

  37. $table->addRow(500);
  38. $table->addCell(2300, $styleCell)->addText('站碼', $fontStyle);

  39. $table ->addCell(2300, $styleCell)->addText('站名', $fontStyle);
  40. $table->addCell(2300, $styleCell)->addText('雨量', $fontStyle);
  41. $table->addCell(2300, $styleCell)->addText('水文站監測類型', $fontStyle);
  42. $conn = mssql_connect($config['mssql'][ 'host'],$config['mssql']['user'],$config['mssql']['password']);

  43. mssql_select_db($config['mssql']['dbname'], $conn);
  44. $stm = date('Y-m-d H:i:s',strtotime('-3 days'));

  45. $sql = "EXEC HNOW05_GETPPSPACE '', '','".$stm."',1,1";
  46. $res=mssql_query($sql);
  47. while($arr = mssql_fetch_array($res)) {

  48. //echo $arr["STCD"]."";
  49. $table->addRow();
  50. $table->addCell(2300)->addText($arr ["STCD"]);
  51. $table->addCell(2300)->addText($arr["STNM"]);
  52. $table->addCell(2300)->addText($arr[" P"]);
  53. if($arr["STTP"] == 'MM'){
  54. $table->addCell(2300)->addText('氣象站');
  55. }else if($arr["STTP"] == 'BB'){
  56. $table->addCell(2300)->addText('蒸發站');
  57. }else if($arr["STTP" ] == 'DD'){
  58. $table->addCell(2300)->addText('堰閘水文站');
  59. }else if($arr["STTP"] == 'TT' ){
  60. $table->addCell(2300)->addText('落潮位元站');
  61. }else if($arr["STTP"] == 'DP'){
  62. $table ->addCell(2300)->addText('泵站');
  63. }else if($arr["STTP"] == 'SS'){
  64. $table->addCell(2300)-> addText('墒情站');
  65. }else if($arr["STTP"] == 'PP'){
  66. $table->addCell(2300)->addText('雨站');
  67. }else if($arr["STTP"] == 'ZZ'){
  68. $table->addCell(2300)->addText('河道水位水文站');
  69. }else if ($arr["STTP"] == 'RR'){
  70. $table->addCell(2300)->addText('水庫水文站');
  71. }else if($arr["STTP" ] == 'ZG'){
  72. $table->addCell(2300)->addText('地下水站');
  73. }else if($arr["STTP"] == 'ZB'){
  74. $table->addCell(2300)->addText('分洪水位站');
  75. }
  76. }
  77. $section->addTextBreak(2);
  78. }else{
  79. }

  80. /******地質災害*******/

  81. if(in_array('3', $a, TRUE)){
  82. $section = $PHPWord->createSection();
  83. $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
  84. $PHPWord->addParagraphStyle('pStyle' , array('align'=>'center', 'spaceAfter'=>100));
  85. $c = "地質災害";
  86. $section->addText($c, 'rStyle', 'pStyle ');
  87. $content="根據市氣象局未來24小時降雨預報和市水利局實時降雨數據,市國土資源局進行了地質災害預報,請有關部門關注 p>

  88. 即時預警訊息,做好地質災害防範工作";

  89. $section->addText($content);
  90. // Add image elements
  91. $section->addImage( "images/image001.jpg", array('width'=>600, 'height'=>480, 'align'=>'center'));
  92. }else{
  93. }

  94. // Save File
  95. $fileName = "word報表".date("YmdHis");
  96. header("Content-type: application/vnd.ms-word");
  97. header("Content-Disposition:attachment;filename=".$fileName.".docx");
  98. header('Cache-Control: max-age=0');
  99. $objWriter = PHPWord_IOFactory::createWriter ($PHPWord, 'Word2007');
  100. $objWriter->save('php://output');
  101. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板