ThinkPHP下實現Word導出

臺網項目中有一個需求是點選震源返回範圍內臺站的各測項信息,思路是先把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('align'=>'center'));
    $table->addCell(2100)->addText($electMediumScore.'('.$ele_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100)->addText($score_count['lt_MediumScore'].'('.$lt_shouldcounts.')',null,array('align'=>'center'));
    $table->addRow();
    $table->addCell(2100,$styleCellColor)->addText("60-75",null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($score_count['xb_LowScore'].'('.$xb_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($electLowScore.'('.$ele_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100,$styleCellColor)->addText($score_count['lt_LowScore'].'('.$lt_shouldcounts.')',null,array('align'=>'center'));
    $table->addRow();
    $table->addCell(2100)->addText("<60",null,array('align'=>'center'));
    $table->addCell(2100)->addText($score_count['xb_FlunkScore'].'('.$xb_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100)->addText($electFlunkScore.'('.$ele_shouldcounts.')',null,array('align'=>'center'));
    $table->addCell(2100)->addText($score_count['lt_FlunkScore'].'('.$lt_shouldcounts.')',null,array('align'=>'center'));

//Add the second conent
$PHPWord->addFontStyle('r2Style', array('bold'=>false, 'name'=>'FangSong', 'size'=>'10.5'));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('注:括號內爲應實時運行的儀器數,括號外爲實時運行的儀器數', 'r2Style', 'pStyle');
$section->addTextBreak(2);

//Add the third conent
$PHPWord->addFontStyle('r3Style', array('bold'=>false, 'name'=>'FangSong', 'size'=>'14'));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('附表:'.$distance.'km範圍內臺項監測預報效能自動評估結果('.date("Y-m-d").')', 'r3Style', 'pStyle');
$section->addTextBreak(0);

// Define the second 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');

$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>\PHPWord_Style_Cell::TEXT_DIR_BTLR);

$fontStyle = array('bold'=>true, 'align'=>'center');
$PHPWord->addParagraphStyle('pStyle', array('bold'=>true,'align'=>'center'));

$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

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

$table->addRow(600);

$table->addCell(1000, $styleCell)->addText('臺站代碼', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('臺站名', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('經度', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('緯度', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('震中距(km)', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('測項代碼', $fontStyle,'pStyle');
$table->addCell(6000, $styleCell)->addText('測項名稱', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('觀測系統評分', $fontStyle,'pStyle');
$table->addCell(4000, $styleCell)->addText('數據質量得分', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('跟蹤應用得分', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('總分', $fontStyle,'pStyle');
$table->addCell(1000, $styleCell)->addText('備註', $fontStyle,'pStyle');
$item_stations = $this->return_eachitems();
for ($i=0; $i <count($item_stations) ; $i++) { 
    # code...
    for ($j=0; $j <count($item_stations[$i]) ; $j++) { 
        //獲得距離
        $distance = $this->getDistance($item_stations[$i][$j]['LATITUDE'], $item_stations[$i][$j]['LONGITUDE'], $param_latitude,$param_longitude);
        if (substr($item_stations[$i][$j]['ITEMID'], 0,1)=='2') {
            $sub='_XB_';
        }
        elseif(substr($item_stations[$i][$j]['ITEMID'], 0,1)=='4') {
            $sub='_LT_';
        }
        elseif(substr($item_stations[$i][$j]['ITEMID'], 0,2)=='31') {
            $sub='_DC_';
        }
        elseif(substr($item_stations[$i][$j]['ITEMID'], 0,2)=='32') {
            $sub='_DDZ_';
        }
        elseif(substr($item_stations[$i][$j]['ITEMID'], 0,2)=='34') {
            $sub='_DDC_';
        }
        elseif(substr($item_stations[$i][$j]['ITEMID'], 0,2)=='91') {
            $sub='_FZ_';
        }
        $item_info=M('','','CONFIG_JC_MANAGEGRADE');
        switch ($sub) {
                    case '_XB_':
                        $eachItems_sql ='SELECT DISTINCT a.ITEMSNAME,a.SYS_EVALUATION,a.DATA_EVALUATION,a.TRA_EVALUATION,a.SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_XB_FINALSCORE a WHERE a.ITEMSID = '.'\''.$item_stations[$i][$j]['ITEMID'].'\''.'  AND a.POINTID='.'\''.$item_stations[$i][$j]['POINTID'].'\''.' AND a.STATIONID='.'\''.$item_stations[$i][$j]['STATIONID'].'\''.' AND ISLASTEST = 1';
                        $items_XB = $item_info->query($eachItems_sql);
                        $table->addRow();
                        $style = array('bgColor'=>'cccccc');
                        $styleCellColor = ($j % 2 == 0) ? $style : '';
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONID'],null,array('align'=>'center'));
                        $table->addCell(3000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LATITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LONGITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($distance,null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['ITEMID'].$item_stations[$i][$j]['POINTID'],null,array('align'=>'center'));
                        $table->addCell(4000,$styleCellColor)->addText($items_XB[0]['ITEMSNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_XB[0]['SYS_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_XB[0]['DATA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_XB[0]['TRA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_XB[0]['SUM_GRADE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText(" ",null,array('align'=>'center'));
                        break;
                    case '_LT_':
                        $eachItems_sql='SELECT DISTINCT a.ITEMSNAME,a.SYS_EVALUATION,a.DATA_EVALUATION,a.TRA_EVALUATION,a.SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_LT_FINALSCORE a WHERE a.ITEMID = '.'\''.$item_stations[$i][$j]['ITEMID'].'\''.'  AND a.POINTID='.'\''.$item_stations[$i][$j]['POINTID'].'\''.' AND a.STATIONID='.'\''.$item_stations[$i][$j]['STATIONID'].'\''.' AND ISLASTEST = 1 ';
                        $items_LT = $item_info->query($eachItems_sql);
                        $table->addRow();
                        $style = array('bgColor'=>'cccccc');
                        $styleCellColor = ($j % 2 == 0) ? $style : '';
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONID'],null,array('align'=>'center'));
                        $table->addCell(3000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LATITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LONGITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($distance,null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['ITEMID'].$item_stations[$i][$j]['POINTID'],null,array('align'=>'center'));
                        $table->addCell(4000,$styleCellColor)->addText($items_LT[0]['ITEMSNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_LT[0]['SYS_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_LT[0]['DATA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_LT[0]['TRA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_LT[0]['SUM_GRADE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText(" ",null,array('align'=>'center'));
                        break;
                    case '_DC_':
                        $eachItems_sql='SELECT DISTINCT a.ITEMSNAME,a.SYS_EVALUATION,a.DATA_EVALUATION,a.TRA_EVALUATION,a.SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DC_FINALSCORE a WHERE a.ITEMID =  '.'\''.$item_stations[$i][$j]['ITEMID'].'\''.'  AND a.POINTID='.'\''.$item_stations[$i][$j]['POINTID'].'\''.' AND a.STATIONID='.'\''.$item_stations[$i][$j]['STATIONID'].'\''.' AND ISLASTEST = 1 ';
                        $items_DC = $item_info->query($eachItems_sql);
                        $table->addRow();
                        $style = array('bgColor'=>'cccccc');
                        $styleCellColor = ($j % 2 == 0) ? $style : '';
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONID'],null,array('align'=>'center'));
                        $table->addCell(3000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LATITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LONGITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($distance,null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['ITEMID'].$item_stations[$i][$j]['POINTID'],null,array('align'=>'center'));
                        $table->addCell(4000,$styleCellColor)->addText($items_DC[0]['ITEMSNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DC[0]['SYS_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DC[0]['DATA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DC[0]['TRA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DC[0]['SUM_GRADE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText(" ",null,array('align'=>'center'));
                        break;
                    case '_DDZ_':
                        $eachItems_sql='SELECT DISTINCT a.ITEMSNAME,a.SYS_EVALUATION,a.DATA_EVALUATION,a.TRA_EVALUATION,a.SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DDZ_FINALSCORE a WHERE a.ITEMID = '.'\''.$item_stations[$i][$j]['ITEMID'].'\''.'  AND a.POINTID='.'\''.$item_stations[$i][$j]['POINTID'].'\''.' AND a.STATIONID='.'\''.$item_stations[$i][$j]['STATIONID'].'\''.' AND ISLASTEST = 1 ';
                        $items_DDZ = $item_info->query($eachItems_sql);
                        $table->addRow();
                        $style = array('bgColor'=>'cccccc');
                        $styleCellColor = ($j % 2 == 0) ? $style : '';
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONID'],null,array('align'=>'center'));
                        $table->addCell(3000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LATITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LONGITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($distance,null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['ITEMID'].$item_stations[$i][$j]['POINTID'],null,array('align'=>'center'));
                        $table->addCell(4000,$styleCellColor)->addText($items_DDZ[0]['ITEMSNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDZ[0]['SYS_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDZ[0]['DATA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDZ[0]['TRA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDZ[0]['SUM_GRADE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText(" ",null,array('align'=>'center'));
                        break;
                    case '_DDC_':
                        $eachItems_sql='SELECT DISTINCT a.ITEMSNAME,a.SYS_EVALUATION,a.DATA_EVALUATION,a.TRA_EVALUATION,a.SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DDC_FINALSCORE a WHERE a.ITEMID = '.'\''.$item_stations[$i][$j]['ITEMID'].'\''.'  AND a.POINTID='.'\''.$item_stations[$i][$j]['POINTID'].'\''.' AND a.STATIONID='.'\''.$item_stations[$i][$j]['STATIONID'].'\''.' AND ISLASTEST = 1';                        
                        $items_DDC = $item_info->query($eachItems_sql);
                        $table->addRow();
                        $style = array('bgColor'=>'cccccc');
                        $styleCellColor = ($j % 2 == 0) ? $style : '';
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONID'],null,array('align'=>'center'));
                        $table->addCell(3000,$styleCellColor)->addText($item_stations[$i][$j]['STATIONNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LATITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['LONGITUDE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($distance,null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($item_stations[$i][$j]['ITEMID'].$item_stations[$i][$j]['POINTID'],null,array('align'=>'center'));
                        $table->addCell(4000,$styleCellColor)->addText($items_DDC[0]['ITEMSNAME'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDC[0]['SYS_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDC[0]['DATA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDC[0]['TRA_EVALUATION'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText($items_DDC[0]['SUM_GRADE'],null,array('align'=>'center'));
                        $table->addCell(1000,$styleCellColor)->addText(" ",null,array('align'=>'center'));
                        break;
                    case '_FZ_':

                        break;
                }

    }

    }
$section->addTextBreak(2);

//Add the fourth conent
$PHPWord->addFontStyle('r4Style', array('bold'=>false, 'name'=>'FangSong', 'size'=>'14'));
$PHPWord->addParagraphStyle('p4Style', array('align'=>'left', 'spaceAfter'=>100));
$section->addText('空間展示功能', 'r4Style', 'p4Style');
$section->addTextBreak(0);  

// Save File
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $date = date('Ymdhis');
        $stationID =$this->SearchCenterStation();
        $savefile = './public/Word/'.$date.'.docx';
        $objWriter->save($savefile);
        echo $savefile;

    }

    /*
    **Query all stations in the 500km range
    **@param $lat $lon
    **@author zdx
    **@date 2017-2-18
    */
    public function SearchAllStations($lat,$lon,$dis){
        //$lat=39.92;
        //$lon=116;
        $mode=M('','','CONFIG_QZDATA');
        $sql="select STATIONID from QZDATA.QZ_DICT_STATIONS where latitude > '".$lat."'-'".$dis."' and latitude < '".$lat."'+'".$dis."' and longitude > '".$lon."'-'".$dis."' and longitude < '".$lon."'+'".$dis."' order by ACOS(SIN(('".$lat."' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('".$lat."' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('".$lon."'* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380";       
        //Check out all stations
        $result = $mode->query($sql);
        //Number of stations
        $station_counts = count($result);
        return $result;
    }
    /*
    **The query returns to the main items grades 
    */
    public function SearchAllItems(){
            //Returns a fractional array
            $score_count = array();
            //Define the number of the main test items
            $item_counts = 0;
            //Get the station within the range
        header("Content-Type: text/html; charset=utf-8");
        if(IS_POST){
        $param_latitude=I('post.lat','','htmlspecialchars');
        $param_longitude=I('post.lng','','htmlspecialchars');
        $param_distance=I('post.radius','','htmlspecialchars');
        }

            $result = $this->SearchAllStations($param_latitude,$param_longitude,$param_distance);

            //Traverse the range of stations ID
            for ($i=0; $i <count($result) ; $i++) { 
            $mode=M('','','CONFIG_QZDATA');
            $item_sql="select distinct a.stationid, a.stationname,b.ITEMID,b.POINTID from qzdata.qz_dict_stations a,qzdata.qz_dict_stationitems b where a.stationid=b.stationid and a.STATIONID='".$result[$i]['STATIONID']."'";
            //Check out the main items in the range station
            $item_result=$mode->query($item_sql);

            /*This cycle is to add up the number of points in each subject
             according to the main items in the range of stations*/
            for ($j=0; $j <count($item_result) ; $j++) { 
                $yy++;
                $stationid=$item_result[$j][STATIONID];
                $itemid=$item_result[$j][ITEMID];
                $pointid=$item_result[$j][POINTID];
                $item_grade=M('','','CONFIG_JC_MANAGEGRADE');
                if (substr($itemid, 0,1)=='2') {
                    $sub='_XB_';
                }
                if (substr($itemid, 0,1)=='4') {
                    $sub='_LT_';
                }
                if (substr($itemid, 0,2)=='31') {
                    $sub='_DC_';
                }
                if (substr($itemid, 0,2)=='32') {
                    $sub='_DDZ_';
                }
                if (substr($itemid, 0,2)=='34') {
                    $sub='_DDC_';
                }
                if (substr($itemid, 0,2)=='91') {
                    $sub='_FZ_';
                }
                switch ($sub) {
                    case '_XB_':
                        $score_sql="SELECT DISTINCT SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_XB_FINALSCORE WHERE ITEMSID = '".$itemid."'  AND POINTID='".$pointid."' AND STATIONID='".$stationid."' AND ISLASTEST = 1";
                        $grade = $item_grade->query($score_sql);
                        if ($grade[0][SUM_GRADE]>=90&&$grade[0][SUM_GRADE]<=100) {
                            $xb_HighScore++;
                        }elseif ($grade[0][SUM_GRADE]>0&&$grade[0][SUM_GRADE]<60) {
                            $xb_FlunkScore++;
                        }elseif ($grade[0][SUM_GRADE]>=60&&$grade[0][SUM_GRADE]<75) {
                            $xb_LowScore++;
                        }elseif ($grade[0][SUM_GRADE]>=75&&$grade[0][SUM_GRADE]<90) {
                            $xb_MediumScore++;
                        }elseif ($grade[0][SUM_GRADE]<=0||$grade[0][SUM_GRADE]>100||$grade[0][SUM_GRADE]==null) {
                            $xb_shouldRuning++;
                        }
                        break;

                    case '_LT_':
                        $score_sql="SELECT DISTINCT SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_LT_FINALSCORE WHERE ITEMID = '".$itemid."'  AND POINTID='".$pointid."' AND STATIONID='".$stationid."' AND ISLASTEST = 1";
                        //var_dump($score_sql);
                        $grade = $item_grade->query($score_sql);
                        //var_dump($grade[0][SUM_GRADE]);
                        if ($grade[0][SUM_GRADE]>=90&&$grade[0][SUM_GRADE]<=100) {
                            $lt_HighScore++;
                        }elseif ($grade[0][SUM_GRADE]>0&&$grade[0][SUM_GRADE]<60) {
                            $lt_FlunkScore++;
                        }elseif ($grade[0][SUM_GRADE]>=60&&$grade[0][SUM_GRADE]<75) {
                            $lt_LowScore++;
                        }elseif ($grade[0][SUM_GRADE]>=75&&$grade[0][SUM_GRADE]<90) {
                            $lt_MediumScore++;
                        }elseif ($grade[0][SUM_GRADE]<=0||$grade[0][SUM_GRADE]>100||$grade[0][SUM_GRADE]==null) {
                            $lt_shouldRuning++;
                        }

                        break;
                    case '_DC_':
                        $score_sql="SELECT DISTINCT SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DC_FINALSCORE WHERE ITEMID = '".$itemid."'  AND POINTID='".$pointid."' AND STATIONID='".$stationid."' AND ISLASTEST = 1";
                        //var_dump($score_sql);
                        $grade = $item_grade->query($score_sql);
                        if ($grade[0][SUM_GRADE]>=90&&$grade[0][SUM_GRADE]<=100) {
                            $dc_HighScore++;
                        }elseif ($grade[0][SUM_GRADE]>0&&$grade[0][SUM_GRADE]<60) {
                            $dc_FlunkScore++;
                        }elseif ($grade[0][SUM_GRADE]>=60&&$grade[0][SUM_GRADE]<75) {
                            $dc_LowScore++;
                        }elseif ($grade[0][SUM_GRADE]>=75&&$grade[0][SUM_GRADE]<90) {
                            $dc_MediumScore++;
                        }elseif ($grade[0][SUM_GRADE]<=0||$grade[0][SUM_GRADE]>100||$grade[0][SUM_GRADE]==null) {
                            $dc_shouldRuning++;
                        }


                        break;
                    case '_DDZ_':
                        $score_sql="SELECT DISTINCT SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DDZ_FINALSCORE WHERE ITEMID = '".$itemid."'  AND POINTID='".$pointid."' AND STATIONID='".$stationid."' AND ISLASTEST = 1";
                        //var_dump($score_sql);
                        $grade = $item_grade->query($score_sql); 
                        if ($grade[0][SUM_GRADE]>=90&&$grade[0][SUM_GRADE]<=100) {
                            $ddz_HighScore++;
                        }elseif ($grade[0][SUM_GRADE]>0&&$grade[0][SUM_GRADE]<60) {
                            $ddz_FlunkScore++;
                        }elseif ($grade[0][SUM_GRADE]>=60&&$grade[0][SUM_GRADE]<75) {
                            $ddz_LowScore++;
                        }elseif ($grade[0][SUM_GRADE]>=75&&$grade[0][SUM_GRADE]<90) {
                            $ddz_MediumScore++;
                        }elseif ($grade[0][SUM_GRADE]<=0||$grade[0][SUM_GRADE]>100||$grade[0][SUM_GRADE]==null) {
                            $ddz_shouldRuning++;
                        }

                        break;
                    case '_DDC_':
                        $score_sql="SELECT DISTINCT SUM_GRADE FROM JC_MANAGEGRADE.MG_CP_DDC_FINALSCORE WHERE ITEMID = '".$itemid."'  AND POINTID='".$pointid."' AND STATIONID='".$stationid."'AND ISLASTEST = 1";
                        $grade = $item_grade->query($score_sql);
                        if ($grade[0][SUM_GRADE]>=90&&$grade[0][SUM_GRADE]<=100) {
                            $ddc_HighScore++;
                        }elseif ($grade[0][SUM_GRADE]>0&&$grade[0][SUM_GRADE]<60) {
                            $ddc_FlunkScore++;
                        }elseif ($grade[0][SUM_GRADE]>=60&&$grade[0][SUM_GRADE]<75) {
                            $ddc_LowScore++;
                        }elseif ($grade[0][SUM_GRADE]>=75&&$grade[0][SUM_GRADE]<90) {
                            $ddc_MediumScore++;
                        }elseif ($grade[0][SUM_GRADE]<=0||$grade[0][SUM_GRADE]>100||$grade[0][SUM_GRADE]==null) {
                            $ddc_shouldRuning++;
                        }

                        break;
                    case '_FZ_':

                        break;


                }
            }
            $item_counts += count($item_result);


           }
        //The grades are stored and returned in the array
        $score_count['xb_shouldRuning'] = $xb_shouldRuning;
        $score_count['lt_shouldRuning'] = $lt_shouldRuning;
        $score_count['ddz_shouldRuning'] = $ddz_shouldRuning;
        $score_count['ddc_shouldRuning'] = $ddc_shouldRuning;
        $score_count['dc_shouldRuning'] = $dc_shouldRuning;
        $score_count['xb_MediumScore'] = $xb_MediumScore;
        $score_count['xb_LowScore'] = $xb_LowScore;
        $score_count['xb_FlunkScore'] = $xb_FlunkScore;
        $score_count['xb_HighScore'] = $xb_HighScore;
        $score_count['lt_MediumScore'] = $lt_MediumScore;
        $score_count['lt_LowScore'] = $lt_LowScore;
        $score_count['lt_FlunkScore'] = $lt_FlunkScore;
        $score_count['lt_HighScore'] = $lt_HighScore;
        $score_count['dc_MediumScore'] = $dc_MediumScore;
        $score_count['dc_LowScore'] = $dc_LowScore;
        $score_count['dc_FlunkScore'] = $dc_FlunkScore;
        $score_count['dc_HighScore'] = $dc_HighScore;
        $score_count['ddc_MediumScore'] = $ddc_MediumScore;
        $score_count['ddc_LowScore'] = $ddc_LowScore;
        $score_count['ddc_FlunkScore'] = $ddc_FlunkScore;
        $score_count['ddc_HighScore'] = $ddc_HighScore;
        $score_count['ddz_MediumScore'] = $ddz_MediumScore;
        $score_count['ddz_LowScore'] = $ddz_LowScore;
        $score_count['ddz_FlunkScore'] = $ddz_FlunkScore;
        $score_count['ddz_HighScore'] = $ddz_HighScore;
        //Number of the main  items
        $score_count['counts'] = $item_counts;
        return $score_count;

    }
    /*
    **返回測點信息
    */
    public function return_eachitems(){
        header("Content-Type: text/html; charset=utf-8");
        if(IS_POST){
        $param_latitude=I('post.lat','','htmlspecialchars');
        $param_longitude=I('post.lng','','htmlspecialchars');
        $param_distance=I('post.radius','','htmlspecialchars');
        }
        $result = $this->SearchAllStations($param_latitude,$param_longitude,$param_distance);
        static $item_result_each = array();
        //Traverse the range of stations ID
        for ($i=0; $i <count($result) ; $i++) { 
        $mode=M('','','CONFIG_QZDATA');
        $item_sql="select distinct a.stationid, a.stationname,a.latitude,a.longitude,b.ITEMID,b.POINTID from qzdata.qz_dict_stations a,qzdata.qz_dict_stationitems b where a.stationid=b.stationid and a.STATIONID='".$result[$i]['STATIONID']."'";
        //Check out the main items in the range station
        $item_result=$mode->query($item_sql);  
        //var_dump($item_result);
        $item_result_each[]=$item_result;
        }
        //var_dump($item_result_each);
        //echo (count($item_result_each));
        return $item_result_each;
    }

    /* 
    * * @desc 根據兩點間的經緯度計算距離 
    * * @param float $lat 緯度值 
    * * @param float $lng 經度值 
    */ 
public function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {  
    $theta = $longitude1 - $longitude2;  
    $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));  
    $miles = acos($miles);  
    $miles = rad2deg($miles);  
    $miles = $miles * 60 * 1.1515;   
    $kilometers = $miles * 1.609344;   
    return round($kilometers,2);  
}
    /* 
    * * @desc 輸出瀏覽器端 
    */ 
    public function sefile(){  
        $data = $_GET[info];  
        $filename = $data;    
        header('Content-Type: application/octet-stream');       
        header('content-disposition:attachment;filename='.basename($filename));
        header('content-length:'.filesize($filename));
        readfile($filename);
        unlink($filename);
    }


}
發佈了74 篇原創文章 · 獲贊 31 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章