search
HomeBackend DevelopmentPHP TutorialPHP uses phpword to generate word documents

php uses phpword to generate word documents:

Related recommendations: PHPWord Chinese user manual download

<p>
<?php
require_once &#39;../libs/PHPWord/PHPWord.php&#39;;
require_once &#39;../libs/PHPWord/PHPWord/IOFactory.php&#39;;
require_once &#39;../../config.php&#39;;
// require_once &#39;../common/conn.php&#39;;</p>
<p>// New Word Document
$PHPWord = new PHPWord();</p>
<p>/**********文本格式的word text.php************/
// New portrait section</p>
<p>//逗号 分割字符串
$arr = $_REQUEST[&#39;arr&#39;];
$a = explode(&#39;,&#39;,$arr);
//echo $arr;
date_default_timezone_set("Asia/Shanghai");//设置一个时区
$tm=date(&#39;Y-m-d H:i:s&#39;);
//exit($tm);
/**********前多日雨量*********/
if(in_array(&#39;1&#39;, $a, TRUE)){
$section = $PHPWord->createSection();
$PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>false, &#39;italic&#39;=>false, &#39;size&#39;=>16));
$PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;, &#39;spaceAfter&#39;=>100));
$c = "前三日雨量报表";
$section->addText($c, &#39;rStyle&#39;, &#39;pStyle&#39;);</p>
<p>$styleTable = array(&#39;borderSize&#39;=>6, &#39;borderColor&#39;=>&#39;006699&#39;, &#39;cellMargin&#39;=>80);
$styleFirstRow = array(&#39;borderBottomSize&#39;=>18, &#39;borderBottomColor&#39;=>&#39;0000FF&#39;, &#39;bgColor&#39;=>&#39;66BBFF&#39;);</p>
<p>// Define cell style arrays
$styleCell = array(&#39;valign&#39;=>&#39;center&#39;);
// Define font style for first row
$fontStyle = array(&#39;bold&#39;=>true, &#39;align&#39;=>&#39;center&#39;);
//设置标题
$PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>true, &#39;italic&#39;=>true, &#39;size&#39;=>16));
$PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;, &#39;spaceAfter&#39;=>100));</p>
<p>// Add table style
$PHPWord->addTableStyle(&#39;myOwnTableStyle&#39;, $styleTable, $styleFirstRow);</p>
<p>// Add table
$table = $section->addTable(&#39;myOwnTableStyle&#39;);</p>
<p>// Add row设置行高
$table->addRow(500);</p>
<p>$table->addCell(2300, $styleCell)->addText(&#39;站码&#39;, $fontStyle);
$table->addCell(2300, $styleCell)->addText(&#39;站名&#39;, $fontStyle);
$table->addCell(2300, $styleCell)->addText(&#39;雨量&#39;, $fontStyle);
$table->addCell(2300, $styleCell)->addText(&#39;水文站监测类型&#39;, $fontStyle);</p>
<p>$conn = mssql_connect($config[&#39;mssql&#39;][&#39;host&#39;],$config[&#39;mssql&#39;][&#39;user&#39;],$config[&#39;mssql&#39;][&#39;password&#39;]);
mssql_select_db($config[&#39;mssql&#39;][&#39;dbname&#39;],$conn);</p>
<p>$stm = date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;-3 days&#39;));
$sql = "EXEC HNOW05_GETPPSPACE &#39;&#39;,&#39;&#39;,&#39;".$stm."&#39;,1,1";
$res=mssql_query($sql);</p>
<p>while($arr = mssql_fetch_array($res)){
//echo $arr["STCD"]."</br>";
$table->addRow();
$table->addCell(2300)->addText($arr["STCD"]);
$table->addCell(2300)->addText($arr["STNM"]);
$table->addCell(2300)->addText($arr["P"]);
if($arr["STTP"] == &#39;MM&#39;){
$table->addCell(2300)->addText(&#39;气象站&#39;);
}else if($arr["STTP"] == &#39;BB&#39;){
$table->addCell(2300)->addText(&#39;蒸发站&#39;);
}else if($arr["STTP"] == &#39;DD&#39;){
$table->addCell(2300)->addText(&#39;堰闸水文站&#39;);
}else if($arr["STTP"] == &#39;TT&#39;){
$table->addCell(2300)->addText(&#39;落潮位站&#39;);
}else if($arr["STTP"] == &#39;DP&#39;){
$table->addCell(2300)->addText(&#39;泵站&#39;);
}else if($arr["STTP"] == &#39;SS&#39;){
$table->addCell(2300)->addText(&#39;墒情站&#39;);
}else if($arr["STTP"] == &#39;PP&#39;){
$table->addCell(2300)->addText(&#39;雨量站&#39;);
}else if($arr["STTP"] == &#39;ZZ&#39;){
$table->addCell(2300)->addText(&#39;河道水位水文站&#39;);
}else if($arr["STTP"] == &#39;RR&#39;){
$table->addCell(2300)->addText(&#39;水库水文站&#39;);
}else if($arr["STTP"] == &#39;ZG&#39;){
$table->addCell(2300)->addText(&#39;地下水站&#39;);
}else if($arr["STTP"] == &#39;ZB&#39;){
$table->addCell(2300)->addText(&#39;分洪水位站&#39;);
}
}
$section->addTextBreak(2);
}else{</p>
<p>}</p>
<p>/******地质灾害*******/
if(in_array(&#39;3&#39;, $a, TRUE)){
$section = $PHPWord->createSection();
$PHPWord->addFontStyle(&#39;rStyle&#39;, array(&#39;bold&#39;=>false, &#39;italic&#39;=>false, &#39;size&#39;=>16));
$PHPWord->addParagraphStyle(&#39;pStyle&#39;, array(&#39;align&#39;=>&#39;center&#39;, &#39;spaceAfter&#39;=>100));
$c = "地质灾害";
$section->addText($c, &#39;rStyle&#39;, &#39;pStyle&#39;);</p>
<p>$content="根据市气象局未来24小时降雨预报和市水利局实时降雨数据,市国土资源局进行了地质灾害预报,
请有关部门关注</p>
<p>实时预警信息,做好地质灾害防范工作";
$section->addText($content);
// Add image elements
$section->addImage("images/image001.jpg", array(&#39;width&#39;=>600, &#39;height&#39;=>480, &#39;align&#39;=>&#39;center&#39;));
}else{</p>
<p>}
// Save File
$fileName = "word报表".date("YmdHis");
header("Content-type: application/vnd.ms-word");
header("Content-Disposition:attachment;filename=".$fileName.".docx");
header(&#39;Cache-Control: max-age=0&#39;);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, &#39;Word2007&#39;);
$objWriter->save(&#39;php://output&#39;);
?>
</p>

The above is the content of php using phpword to generate word documents. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!

Related articles:

How to export and generate word from php

Generate a word document from a web page in a php program and provide downloadable example code

Three ways to implement PHP to generate a word document

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.