phpword生成word文档

php使用phpword生成word文档的例子,供大家学习参考。

//注意:要将PHPWord\PHPWord\Template.php中的setValue方法下的编码修改成:$replace = iconv('gbk', 'utf-8',$replace);

其他的文件也要改成这种样式的。否则出现中文乱码。
 

复制代码代码示例:

<?php
require_once '../libs/PHPWord/PHPWord.php';
require_once '../libs/PHPWord/PHPWord/IOFactory.php';
require_once '../../config.php';
// require_once '../common/conn.php';

// New Word Document
$PHPWord = new PHPWord();

/**********文本格式的word text.php************/
// New portrait section

//逗号 分割字符串
$arr = $_REQUEST['arr'];
$a = explode()(',',$arr);
//echo $arr;
date_default_timezone_set("Asia/Shanghai");//设置一个时区
$tm=date('Y-m-d H:i:s');
//exit($tm);
/**********前多日雨量*********/
if(in_array('1', $a, TRUE)){
$section = $PHPWord->createSection();
$PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$c = "前三日雨量报表";
$section->addText($c, 'rStyle', 'pStyle');

$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');

// Define cell style arrays
$styleCell = array('valign'=>'center');
// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');
//设置标题
$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设置行高
$table->addRow(500);

$table->addCell(2300, $styleCell)->addText('站码', $fontStyle);
$table->addCell(2300, $styleCell)->addText('站名', $fontStyle);
$table->addCell(2300, $styleCell)->addText('雨量', $fontStyle);
$table->addCell(2300, $styleCell)->addText('水文站监测类型', $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."',1,1";
$res=mssql_query($sql);

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"] == 'MM'){
$table->addCell(2300)->addText('气象站');
}else if($arr["STTP"] == 'BB'){
$table->addCell(2300)->addText('蒸发站');
}else if($arr["STTP"] == 'DD'){
$table->addCell(2300)->addText('堰闸水文站');
}else if($arr["STTP"] == 'TT'){
$table->addCell(2300)->addText('落潮位站');
}else if($arr["STTP"] == 'DP'){
$table->addCell(2300)->addText('泵站');
}else if($arr["STTP"] == 'SS'){
$table->addCell(2300)->addText('墒情站');
}else if($arr["STTP"] == 'PP'){
$table->addCell(2300)->addText('雨量站');
}else if($arr["STTP"] == 'ZZ'){
$table->addCell(2300)->addText('河道水位水文站');
}else if($arr["STTP"] == 'RR'){
$table->addCell(2300)->addText('水库水文站');
}else if($arr["STTP"] == 'ZG'){
$table->addCell(2300)->addText('地下水站');
}else if($arr["STTP"] == 'ZB'){
$table->addCell(2300)->addText('分洪水位站');
}
}
$section->addTextBreak(2);
}else{

}

/******地质灾害*******/
if(in_array('3', $a, TRUE)){
$section = $PHPWord->createSection();
$PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$c = "地质灾害";
$section->addText($c, 'rStyle', 'pStyle');

$content="根据市气象局未来24小时降雨预报和市水利局实时降雨数据,市国土资源局进行了地质灾害预报,请有关部门关注

实时预警信息,做好地质灾害防范工作";
$section->addText($content);
// Add image elements
$section->addImage("images/image001.jpg", array('width'=>600, 'height'=>480, 'align'=>'center'));
}else{

}
// Save File
$fileName = "word报表".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');
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHPWord 是一款用于动态生成和操作 Microsoft Word 文档的 PHP 库。在生成 Word 文档时,有时候需要在文档的开头添加一个目录,以便读者可以快速查找到各个部分的内容。下面是使用 PHPWord 生成目录的步骤: 1. 首先,我们需要创建一个新的 PHPWord 对象: ```php $phpWord = new \PhpOffice\PhpWord\PhpWord(); ``` 2. 在创建文档的过程中,我们可以使用 `$phpWord->addTableOfContents` 方法来添加目录。该方法接受一个参数,用于设置目录的格式。例如,可以指定标题的字体、字号和样式: ```php $section = $phpWord->addSection(); $section->addText('Contents', array('size' => 24, 'bold' => true)); $tableOfContents = $section->addTableOfContents(array('fontSize' => 12, 'marginLeft' => 96, 'tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT)); ``` 3. 在生成文档的过程中,可以使用 `$section->addText` 方法来添加正文内容。 ```php $section->addText('Section 1'); $section->addText('Section 2'); $section->addText('Section 3'); ``` 4. 最后,在生成文档的任意位置,可以使用 `$section->addTOC` 方法来插入目录。这将创建一个可点击的目录链接。 ```php $section->addTOC(); ``` 5. 最后,我们将生成的 Word 文档保存到服务器或者下载到本地: ```php $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $writer->save('path/to/save/document.docx'); ``` 以上就是使用 PHPWord 生成目录的基本步骤。通过添加目录,我们可以方便地在 Word 文档中导航和查找各个部分的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值