ThinkPHP下实现Word导出

在台网项目中,为满足震源回波信息导出需求,本文档介绍如何利用ThinkPHP框架处理多维数组,将查询到的台站及测项信息填充到预先设计的Word模板中,提高导出效率。通过调整查询和模板绘制策略,避免了复杂的数据匹配,实现了高效的数据写入。
摘要由CSDN通过智能技术生成

台网项目中有一个需求是点选震源返回范围内台站的各测项信息,思路是先把Word模板绘制好,然后-查询台站信息写入模板中。Word导出效果图如下。
这里写图片描述
实现这个模块的困难在于对多位数组的操作。在查询完点选范围内的所有台站信息后,在根据这些信息逐一去查询测项名称和分数信息。最开始由于思维局限,考虑的是把逐一查询的信息和台站信息匹配存放在同一一个数组中,然后单独在绘制word模板函数中再把它遍历出来。先不说这本身已经把效率变得很慢,其次是这个查询出结果的数组已经是多于三维的数组了,无法把需要的信息组合在一起。
思路重新捋一捋后,换了一种办法,是先把台站基本信息返回给Word模板绘制函数,然后再Word模板函数中逐一查询得分信息,直接绘制在表格中。
下面是代码:

<?php
/*
**@function Out put the word into Public/Word
**@need PHPWord Class in Include/Library/Org/Util/PHPWord
**@author zdx
**@date the last modified time 2017-2-20
**@bug fixs by zdx 2017-2-22 除去导出中出现的辅助测项和应实时运行仪器数与实际值不匹配
*/
namespace Home\Controller;
use Think\Controller;
ini_set('memory_limit','1024M');
ini_set('max_execution_time','86400');
class OutWordController extends AuthController{
    
    public function index(){
    
        header('Content-Type:text/html; charset=utf8');
        import('Org.Util.PHPWord');
        import('Org.Util.PHPWord.IOFactory');
        // New Word Document
        if(IS_POST){
        $param_latitude=I('post.lat','','htmlspecialchars');
        $param_longitude=I('post.lng','','htmlspecialchars');
        $param_distance=I('post.radius','','htmlspecialchars');
        }
        $distance=$param_distance*100;
//$PHPWord = new PHPWord();
$PHPWord = new \PHPWord();
// New portrait section
$section = $PHPWord->createSection();
//Get time stamp
$year = date("Y");
$month = date("m");
$day = date("d");
$hour = date("G");
$minute = date("i");

// Add header
$header = $section->createHeader();
$table = $header->addTable();
$table->addRow();
$table->addCell(4500)->addText($year.'年'.$month.'月'.$day.'日');
$table->addCell(4500)->addText('测试版',null,array('align'=>'right'));

// Add listitem elements
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'FangSong', 'size'=>'14'));
$PHPWord->addParagraphStyle('P-Style', array('spaceAfter'=>95));
$listStyle = array('listType'=>\PHPWord_Style_ListItem::TYPE_NUMBER_NESTED);
$section->addListItem('数据质量评估情况', 0, 'myOwnStyle', $listStyle, 'P-Style');

// Prepare the score from SearchAllItems()
$score_count =$this->SearchAllItems();
$HighScore = $score_count['xb_HighScore']+$score_count['lt_HighScore']+$score_count['dc_HighScore']+$score_count['ddc_HighScore']+$score_count['ddz_HighScore'];
$MediumScore = $score_count['xb_MediumScore']+$score_count['lt_MediumScore']+$score_count['dc_MediumScore']+$score_count['ddc_MediumScore']+$score_count['ddz_MediumScore'];
$LowScore = $score_count['xb_LowScore']+$score_count['lt_LowScore']+$score_count['dc_LowScore']+$score_count['ddc_LowScore']+$score_count['ddz_LowScore'];
$FlunkScore = $score_count['xb_FlunkScore']+$score_count['lt_FlunkScore']+$score_count['dc_FlunkScore']+$score_count['ddc_FlunkScore']+$score_count['ddz_FlunkScore'];
$electHighScore = $score_count['dc_HighScore']+$score_count['ddc_HighScore']+$score_count['ddz_HighScore'];
$electMediumScore = $score_count['dc_MediumScore']+$score_count['ddc_MediumScore']+$score_count['ddz_MediumScore'];
$electLowScore = $score_count['dc_LowScore']+$score_count['ddc_LowScore']+$score_count['ddz_LowScore'];
$electFlunkScore = $score_count['dc_FlunkScore']+$score_count['ddc_FlunkScore']+$score_count['ddz_FlunkScore'];
$ele_shouldcounts = $score_count['ddz_shouldRuning']+$score_count['ddc_shouldRuning']+$score_count['dc_shouldRuning']+$electHighScore+$electMediumScore+$electLowScore+$electFlunkScore;
$xb_shouldcounts = $score_count['xb_shouldRuning']+$score_count['xb_HighScore']+$score_count['xb_MediumScore']+$score_count['xb_LowScore']+$score_count['xb_FlunkScore'];
$lt_shouldcounts = $score_count['lt_shouldRuning']+$score_count['lt_HighScore']+$score_count['lt_MediumScore']+$score_count['lt_LowScore']+$score_count['lt_FlunkScore'];
$ItemCount = $ele_shouldcounts+$xb_shouldcounts+$lt_shouldcounts;
//Null assignment of data to zero
if ($score_count['xb_HighScore']==null) {
    $score_count['xb_HighScore'] = 0;
}
if ($score_count['xb_MediumScore']==null) {
    $score_count['xb_MediumScore'] = 0;
}
if ($score_count['xb_LowScore']==null ){
    $score_count['xb_LowScore'] = 0;
}
if ($score_count['xb_FlunkScore']==null ){
    $score_count['xb_FlunkScore'] = 0;
}
if ($score_count['lt_HighScore']==null) {
    $score_count['lt_HighScore'] = 0;
}
if ($score_count['lt_MediumScore']==null) {
   $score_count['lt_MediumScore'] = 0;
}
if ($score_count['lt_LowScore']==null) {
    $score_count['lt_LowScore'] = 0;
}
if ($score_count['lt_FlunkScore']==null) {
    $score_count['lt_FlunkScore'] = 0;
}
// Add the main text elements
$PHPWord->addFontStyle('rmStyle', array('bold'=>false, 'name'=>'FangSong', 'size'=>'14'));
$PHPWord->addParagraphStyle('pmStyle', array('align'=>'left','spacing'=>150, 'spaceAfter'=>100,'spaceBefore'=>50));
$section->addText(' 震中距离'.$distance.'km范围内,共检测到'.$ItemCount.'台项主观测数据,检测预报效能得分为90~100的有'.$HighScore.'项,75~90的有'.$MediumScore.'项,60~75的有'.$LowScore.'项,60分以下的有'.$FlunkScore.'项(附件1)。', 'rmStyle', 'pmStyle');
$section->addTextBreak(2);

// Define the  first table style arrays
$styleTable = array('alignMent' => 'center','borderTopSize'=>16, 'borderTopColor'=>'434343','borderBottomSize'=>16,'borderBottomColor'=>'434343', 'cellMargin'=>80,'borderLeftColor'=>'ffffff','borderRightColor'=>'ffffff','borderInsideVColor'=>'ffffff');
$styleFirstRow = array('borderBottomSize'=>16, 'borderBottomColor'=>'434343', 'bgColor'=>'ffffff');

// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>\PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');
$PHPWord->addParagraphStyle('pStyle', array('bold'=>true,'align'=>'center'));
// Add the  first table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

// Add table
$table = $section->addTable('myOwnTableStyle');

// Add row
$table->addRow(450);

// Add cells
$table->addCell(2100, $styleCell)->addText('监测预报效能自动评估得分', $fontStyle,'pStyle');
$table->addCell(2100, $styleCell)->addText('形变学科', $fontStyle,'pStyle');
$table->addCell(2100, $styleCell)->addText('电磁学科', $fontStyle,'pStyle');
$table->addCell(2100, $styleCell)->addText('流体学科', $fontStyle,'pStyle');


// Add more rows / cells
$styleCellColor = array('bgColor'=>'cccccc');
    $table->addRow();
    $table->addCell(2100,$styleCellColor)->addText("90-100",null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($score_count['xb_HighScore'].'('.$xb_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($electHighScore.'('.$ele_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($score_count['lt_HighScore'].'('.$lt_shouldcounts.')',null,array('align'=>'center'));
    $table->addRow();
    $table->addCell(2100)->addText("75-90",null,array('align'=>'center'));
    $table->addCell(2100)->addText($score_count['xb_MediumScore'].'('.$xb_shouldcounts.')',null,array(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值