Example of php using phpword to generate word documents

WBOY
Release: 2016-07-25 09:04:00
Original
1798 people have browsed it
  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();< ;/p>
  7. /**********Text format 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");//Set a time zone
  14. $tm=date('Y-m-d H:i:s' );
  15. //exit($tm);
  16. /**********Rain volume in recent 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', 'spaceAfter'=>100));
  21. $c = "Previous three days' 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 the 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. < p>// 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('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."',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 Station');
  57. }else if($arr["STTP"] == 'DD'){
  58. $table->addCell(2300)->addText('Weir Gate Hydrological Station');
  59. }else if($arr["STTP"] == 'TT'){
  60. $table ->addCell(2300)->addText('Ebb Tide 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('Pumping Station');
  65. }else if($arr["STTP"] == 'PP'){
  66. $table->addCell(2300)->addText('Rain Gauge Station');
  67. }else if($arr["STTP" ] == 'ZZ'){
  68. $table->addCell(2300)->addText('River water level 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('Underground water station');
  73. }else if($arr["STTP"] == 'ZB'){
  74. $table->addCell(2300)->addText('Flood level Station');
  75. }
  76. }
  77. $section->addTextBreak(2);
  78. }else{

  79. }

  80. /******Geological disaster*******/

  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 Hazards";
  86. $section->addText($c, 'rStyle', 'pStyle');

  87. $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

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

Copy code


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!