Example of using phpword to generate a Word document in php

Source: Internet
Author: User
Tags phpword
Example of using phpword to generate a Word document in php

  1. Require_once '../libs/PHPWord. php ';

  2. Require_once '../libs/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. // Comma-separated string

  10. $ Arr = $ _ REQUEST ['arr'];
  11. $ A = explode (',', $ arr );
  12. // Echo $ arr;
  13. Date_default_timezone_set ("Asia/Shanghai"); // you can specify a time zone.
  14. $ Tm = date ('Y-m-d H: I: s ');
  15. // Exit ($ tm );
  16. /************ Rainfall of the previous multiple days *********/
  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', 'space' => 100 ));
  21. $ C = "rainfall report for the first three days ";
  22. $ Section-> addText ($ c, 'rstyle', 'pstyle ');

  23. $ StyleTable = array ('bordersize' => 6, 'bordercolor' => '123', '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. // Set the title
  30. $ PHPWord-> addFontStyle ('rstyle', array ('bold '=> true, 'italic' => true, 'size' => 16 ));
  31. $ PHPWord-> addParagraphStyle ('pstyle', array ('align '=> 'center', 'space' => 100 ));

  32. // Add table style

  33. $ PHPWord-> addTableStyle ('myowntablestyle', $ styleTable, $ styleFirstRow );

  34. // Add table

  35. $ Table = $ section-> addTable ('myowntablestyle ');

  36. // Add row to set the row height

  37. $ Table-> addRow (500 );

  38. $ Table-> addCell (2300, $ styleCell)-> addText ('site code', $ fontStyle );

  39. $ Table-> addCell (2300, $ styleCell)-> addText ('site name', $ fontStyle );
  40. $ Table-> addCell (2300, $ styleCell)-> addText ('rainfall ', $ fontStyle );
  41. $ Table-> addCell (2300, $ styleCell)-> addText ('hydrological station monitoring type', $ fontStyle );

  42. $ Conn = mssql_connect ($ config ['mssql'] ['host'], $ config ['mssql'] ['user'], $ config ['SQL'] ['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 ('weather station ');
  55. } Else if ($ arr ["STTP"] = 'BB '){
  56. $ Table-> addCell (2300)-> addText ('Evaporation site ');
  57. } Else if ($ arr ["STTP"] = 'DD '){
  58. $ Table-> addCell (2300)-> addText ('dayu hydrological station ');
  59. } Else if ($ arr ["STTP"] = 'TT '){
  60. $ Table-> addCell (2300)-> addText ('tidal slots ');
  61. } Else if ($ arr ["STTP"] = 'DP '){
  62. $ Table-> addCell (2300)-> addText ('pumping station ');
  63. } Else if ($ arr ["STTP"] = 'SS '){
  64. $ Table-> addCell (2300)-> addText ('Site of moisture ');
  65. } Else if ($ arr ["STTP"] = 'pp '){
  66. $ Table-> addCell (2300)-> addText ('Rain station ');
  67. } Else if ($ arr ["STTP"] = 'zz '){
  68. $ Table-> addCell (2300)-> addText ('River water level and hydrological station ');
  69. } Else if ($ arr ["STTP"] = 'rr '){
  70. $ Table-> addCell (2300)-> addText ('Reservoir hydrological station ');
  71. } Else if ($ arr ["STTP"] = 'zg '){
  72. $ Table-> addCell (2300)-> addText ('Groundwater station ');
  73. } Else if ($ arr ["STTP"] = 'zb '){
  74. $ Table-> addCell (2300)-> addText ('flood location site ');
  75. }
  76. }
  77. $ Section-> addTextBreak (2 );
  78. } Else {

  79. }

  80. /******* Geological disasters *******/

  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', 'space' => 100 ));
  85. $ C = "geological disasters ";
  86. $ Section-> addText ($ c, 'rstyle', 'pstyle ');

  87. $ Content = "according to the rainfall forecast of the municipal meteorological administration and the real-time rainfall data of the municipal water conservancy bureau in the next 24 hours, the Municipal Bureau of Land and Resources has made a geological disaster forecast. please pay attention to it.

  88. Real-time warning information to prevent geological disasters ";

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.