Home Backend Development PHP Tutorial Example of php using phpword to generate word documents

Example of php using phpword to generate word documents

Jul 25, 2016 am 09:04 AM

  1. <p><?php
  2. require_once '../libs/PHPWord/PHPWord.php';
  3. require_once '../libs/PHPWord/PHPWord/IOFactory.php';
  4. require_once '. ./../config.php';
  5. // require_once '../common/conn.php';</p>
  6. <p>// New Word Document
  7. $PHPWord = new PHPWord();&lt ;/p>
  8. <p>/**********Text format word text.php************/
  9. // New portrait section</p>
  10. <p>//Comma separated string
  11. $arr = $_REQUEST['arr'];
  12. $a = explode(',',$arr);
  13. //echo $arr;
  14. date_default_timezone_set("Asia/Shanghai");//Set a time zone
  15. $tm=date('Y-m-d H:i:s' );
  16. //exit($tm);
  17. /**********Rain volume in recent days*********/
  18. if(in_array('1', $a, TRUE)){
  19. $section = $PHPWord->createSection();
  20. $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
  21. $PHPWord->addParagraphStyle('pStyle' , array('align'=>'center', 'spaceAfter'=>100));
  22. $c = "Previous three days' rainfall report";
  23. $section->addText($c, 'rStyle', 'pStyle');</p>
  24. <p>$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
  25. $styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');</p>
  26. <p>// Define cell style arrays
  27. $ styleCell = array('valign'=>'center');
  28. // Define font style for first row
  29. $fontStyle = array('bold'=>true, 'align'=>'center');
  30. //Set the title
  31. $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
  32. $PHPWord-> addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));</p>
  33. <p>// Add table style
  34. $PHPWord->addTableStyle ('myOwnTableStyle', $styleTable, $styleFirstRow);</p>
  35. <p>// Add table
  36. $table = $section->addTable('myOwnTableStyle');</p>
  37. < p>// Add row to set the row height
  38. $table->addRow(500);</p>
  39. <p>$table->addCell(2300, $styleCell)->addText('site code ', $fontStyle);
  40. $table->addCell(2300, $styleCell)->addText('station name', $fontStyle);
  41. $table->addCell(2300, $styleCell)->addText ('Rainfall', $fontStyle);
  42. $table->addCell(2300, $styleCell)->addText('Hydrological station monitoring type', $fontStyle);</p>
  43. <p>$conn = mssql_connect($config['mssql']['host'],$config['mssql']['user'],$config['mssql']['password']);
  44. mssql_select_db($config[' mssql']['dbname'],$conn);</p>
  45. <p>$stm = date('Y-m-d H:i:s',strtotime('-3 days'));
  46. $sql = "EXEC HNOW05_GETPPSPACE '','','".$stm."',1,1";
  47. $res=mssql_query($sql);</p>
  48. <p>while($arr = mssql_fetch_array ($res)){
  49. //echo $arr["STCD"]."</br>";
  50. $table->addRow();
  51. $table->addCell(2300)->addText ($arr["STCD"]);
  52. $table->addCell(2300)->addText($arr["STNM"]);
  53. $table->addCell(2300)->addText($ arr["P"]);
  54. if($arr["STTP"] == 'MM'){
  55. $table->addCell(2300)->addText('Weather Station');
  56. }else if ($arr["STTP"] == 'BB'){
  57. $table->addCell(2300)->addText('Evaporation Station');
  58. }else if($arr["STTP"] == 'DD'){
  59. $table->addCell(2300)->addText('Weir Gate Hydrological Station');
  60. }else if($arr["STTP"] == 'TT'){
  61. $table ->addCell(2300)->addText('Ebb Tide Station');
  62. }else if($arr["STTP"] == 'DP'){
  63. $table->addCell(2300)-&gt ;addText('Pumping Station');
  64. }else if($arr["STTP"] == 'SS'){
  65. $table->addCell(2300)->addText('Pumping Station');
  66. }else if($arr["STTP"] == 'PP'){
  67. $table->addCell(2300)->addText('Rain Gauge Station');
  68. }else if($arr["STTP" ] == 'ZZ'){
  69. $table->addCell(2300)->addText('River water level hydrological station');
  70. }else if($arr["STTP"] == 'RR'){
  71. $table->addCell(2300)->addText('Reservoir Hydrological Station');
  72. }else if($arr["STTP"] == 'ZG'){
  73. $table->addCell(2300 )->addText('Underground water station');
  74. }else if($arr["STTP"] == 'ZB'){
  75. $table->addCell(2300)->addText('Flood level Station');
  76. }
  77. }
  78. $section->addTextBreak(2);
  79. }else{</p>
  80. <p>}</p>
  81. <p>/******Geological disaster*******/
  82. if(in_array('3', $a, TRUE)){
  83. $section = $PHPWord->createSection();
  84. $PHPWord ->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
  85. $PHPWord->addParagraphStyle('pStyle', array ('align'=>'center', 'spaceAfter'=>100));
  86. $c = "Geological Hazards";
  87. $section->addText($c, 'rStyle', 'pStyle'); </p>
  88. <p>$content="Based on the 24-hour rainfall forecast of the Municipal Meteorological Bureau and the real-time rainfall data of the Municipal Water Conservancy Bureau, the Municipal Land and Resources Bureau has made a geological disaster forecast. Please pay attention to the relevant departments</p>
  89. <p>Real-time warning information to prevent geological disasters";
  90. $section->addText($content);
  91. // Add image elements
  92. $section->addImage("images/image001.jpg" , array('width'=>600, 'height'=>480, 'align'=>'center'));
  93. }else{</p>
  94. <p>}
  95. // Save File
  96. $fileName = "word report".date("YmdHis");
  97. header("Content-type: application/vnd.ms-word");
  98. header("Content-Disposition:attachment;filename=".$ fileName.".docx");
  99. header('Cache-Control: max-age=0');
  100. $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  101. $objWriter->save(' php://output');
  102. ?></p>
Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles