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

來源:互聯網
上載者:User
  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小時降雨預報和市水利局即時降雨資料,市國土資源局進行了地質災害預報,請有關部門關注

  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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.