欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php使用phpword生成word文档的例子

程序员文章站 2022-04-13 13:42:04
...
  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. ?>
复制代码