Example of PHP using Phpword to generate Word documents

Source: Internet
Author: User
Tags phpword
  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************/in text format

  8. New Portrait section

  9. Comma-delimited string

  10. $arr = $_request[' arr '];
  11. $a = explode (', ', $arr);
  12. Echo $arr;
  13. Date_default_timezone_set ("Asia/shanghai");//Set a time zone
  14. $tm =date (' y-m-d h:i:s ');
  15. Exit ($TM);
  16. Multi-day rainfall *********/before/**********
  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 = "Previous 3rd rainfall report";
  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. Set Title
  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 Set Line height

  37. $table->addrow (500);

  38. $table->addcell (2300, $styleCell)->addtext (' station code ', $fontStyle);

  39. $table->addcell (2300, $styleCell)->addtext (' Station 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 [' 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. "',";
  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 Station ');
  57. }else if ($arr ["STTP"] = = ' DD ') {
  58. $table->addcell (2300)->addtext (' Weir Sluice hydrologic station ');
  59. }else if ($arr ["STTP"] = = ' TT ') {
  60. $table->addcell (2300)->addtext (' Ebb position Station ');
  61. }else if ($arr ["STTP"] = = ' DP ') {
  62. $table->addcell (2300)->addtext (' Pumping Station ');
  63. }else if ($arr ["STTP"] = = ' SS ') {
  64. $table->addcell (2300)->addtext (' Moisture station ');
  65. }else if ($arr ["STTP"] = = ' PP ') {
  66. $table->addcell (2300)->addtext (' Rainfall Station ');
  67. }else if ($arr ["STTP"] = = ' ZZ ') {
  68. $table->addcell (2300)->addtext (' river water level hydrologic 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 (' min flood Level station ');
  75. }
  76. }
  77. $section->addtextbreak (2);
  78. }else{

  79. }

  80. /****** Geological Hazard *******/

  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 = "geological hazard";
  86. $section->addtext ($c, ' Rstyle ', ' pstyle ');

  87. $content = "According to the City Meteorological Bureau in the next 24-hour rainfall forecast and the city water Bureau real-time rainfall data, the city land and resources department conducted a geological disaster forecast, please the relevant departments concerned

  88. Real-time warning information, good geological disaster prevention work ";

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

Copy Code
  • Related Article

    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.