PHP example of using Phpword Library to generate Word documents

Source: Internet
Author: User
Tags mssql php example phpword

<?phprequire_once '. /libs/phpword/phpword/iofactory.php '; Require_once '. /.. /config.php '; $PHPWord = new Phpword ();/********** text Format word text.php************///new portrait section//comma-delimited string $arr = $_request[' arr ']; $a = explode (', ', $arr),//echo $arr;d ate_default_timezone_set ("Asia/shanghai");//Set a time zone $tm=date (' y-m-d h:i:s '); $section = $PHPWord->createsection (); $PHPWord->addfontstyle (' Rstyle ', Array (' bold ' =>false, ' Italic ' =>false, ' size ' =>16); $PHPWord->addparagraphstyle (' Pstyle ', Array (' align ' = ' center ', ' SpaceAfter ' =>100); $c = "Previous 3rd rainfall report"; $section->addtext ($c, ' Rstyle ', ' pstyle ');//Generate Table $styletable = Array (' Bordersize ' =>6, ' bordercolor ' = ' 006699 ', ' Cellmargin ' =>80); $styleFirstRow = Array (' borderbottomsize ' = , ' borderbottomcolor ' = ' 0000FF ', ' bgColor ' = ' 66BBFF ');//Define cell style Arrays$stylecell = Array (' valign ' = > ' center ');//Define font style for first row$fontstyle = Array (' bold ' =>true, ' align ' = ' center ');//Set title $ Phpword->aDdfontstyle (' Rstyle ', Array (' bold ' =>true, ' italic ' =>true, ' size ' =>16)); $PHPWord->addparagraphstyle (' Pstyle ', Array (' align ' = ' center ', ' SpaceAfter ' =>100));//ADD Table Style$phpword->addtablestyle (' Myowntablestyle ', $styleTable, $styleFirstRow);//Add table$table = $section->addtable (' Myowntablestyle ');//Add Row set line High $table->addrow ($), $table->addcell (2300, $styleCell)->addtext (' station code ', $fontStyle); $table Addcell (2300, $styleCell)->addtext (' Station name ', $fontStyle); $table->addcell (2300, $styleCell)->addtext (' Rainfall ', $  FontStyle); $table->addcell (2300, $styleCell)->addtext (' Hydrological station monitoring type ', $fontStyle); $conn = Mssql_connect ($config [' MSSQL '] [' host '], $config [' MSSQL ' [' User '], $config [' MSSQL '] [' password ']); mssql_ select_db ($config [' MSSQL '] [' dbname '], $conn); $stm = Date (' y-m-d h:i:s ', Strtotime ('-3 days ')); $sql = "EXEC hnow05_ Getppspace ', ', ', '. $stm. ', ', ' $res =mssql_query ($sql); while ($arr = Mssql_fetch_array ($res)) {$table->addrow () ; $table->addcell (2300)->addtext ($arr ["STCD"]); $table->addcell (2300)->addtext ($arr ["STNM"]); $table->addcell (2300)->addtext ($arr ["P"]); if ($arr ["STTP"] = = ' MM ') {$table->addcell (2300)->addtext (' Weather Station ');} else if ($arr ["STTP"] = = ' BB ') {$table->addcell (2300)->addtext (' Evaporation Station ');}  else if ($arr ["STTP"] = = ' DD ') {$table->addcell (2300)->addtext (' Weir Gate hydrologic Station ');  }else if ($arr ["STTP"] = = ' TT ') {$table->addcell (2300)->addtext (' Ebb position Station ');  }else if ($arr ["STTP"] = = ' DP ') {$table->addcell (2300)->addtext (' Pumping Station ');  }else if ($arr ["STTP"] = = ' SS ') {$table->addcell (2300)->addtext (' Moisture station ');  }else if ($arr ["STTP"] = = ' PP ') {$table->addcell (2300)->addtext (' Rainfall Station ');  }else if ($arr ["STTP"] = = ' ZZ ') {$table->addcell (2300)->addtext (' river water level hydrologic Station ');  }else if ($arr ["STTP"] = = ' RR ') {$table->addcell (2300)->addtext (' Reservoir hydrological Station ');  }else if ($arr ["STTP"] = = ' ZG ') {$table->addcell (2300)->addtext (' Groundwater station ');  }else if ($arr ["STTP"] = = ' ZB ') {$table->addcell (2300)->addtext (' sub-flood level station ');}} $section->addtextbreak (2), $section = $PHPWord->createsection (); $PHPWord->addfontstyle (' Rstyle ', Array ( ' Bold ' =>false, ' italic ' =>false, ' size ' =>16); $PHPWord->addparagraphstyle (' Pstyle ', Array (' align ' = ' Center ', ' SpaceAfter ' =>100); $c = "geological hazard"; $section->addtext ($c, ' Rstyle ', ' pstyle '); $content = " According to the 24-hour rainfall forecast and the real-time rainfall data of Municipal Meteorological Bureau, the city Land and Resources Bureau carried out the geological disaster forecast, and asked the relevant departments to pay close attention to real-time warning information and geological disaster prevention work.  $section->addtext ($content); ADD image Elements$section->addimage ("Images/image001.jpg", Array (' width ' =>600, ' height ' =>480, ' align ' = > ' center ');//Save File$filename = "word report". Date ("Ymdhis"); Header ("Content-type:application/vnd.ms-word"); Header ("Content-disposition:attachment;filename=". $fileName. ". Docx "); Header (' cache-control:max-age=0 '); $objWriter = Phpword_iofactory::createwriter ($PHPWord, ' Word2007 '); $objWriter- >save (' php://output ');? >

Note: To change the encoding under the SetValue method in phpword\phpword\template.php to $replace = Iconv (' GBK ', ' utf-8 ', $replace), the other files are changed to this style. Otherwise, there is a garbled Chinese.

PHP example of using Phpword Library to generate Word documents

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.