導出結果:php
首先,到官網下載PHPExcel插件包,下載後文件夾以下:html
將Classes文件夾放入到項目公共方法內。web
新建控制器(訪問導出的方法):EntryandexitControllerapp
<?php
namespace app\modules\reportmanagement\controllers;
use Yii;
use yii\web\Controller;
use app\modules\reportmanagement\components\Summaryexport;
class EntryandexitController extends BaseController
{
public function actionIndex(){
//查詢需導出的數據
$result = array();
//調用導出方法
Summaryexport::export($result);
}
}框架
新建控制器(公共方法):Summaryexportyii
<?php
namespace app\modules\reportmanagement\components;
//引入PHPExcel插件
require dirname(dirname(__FILE__)).'/components/phpexcel/PHPExcel.php';
class Summaryexport
{
public static function export($result){
header("content-type:text/html;charset=utf-8");
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
//建立sheet工做薄
$objPHPExcel = new \PHPExcel();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
//$result封裝(方式選其一便可) -> 此數據可用此方式輸出表格或使用單元格賦值兩種方式(表格數據)
//方式一
$objWorksheet->fromArray(
array(
array('航次', 'E人數','W人數','總人數','增加率','E商人','W商人','總商人數','團體','通常'),
array('10010','10','12','=SUM(B2:B3)','=SUM((B2:B3)/B3)','10','2','12','12','12'),
array('10020','10','12','=SUM(B2:B3)','=SUM((B2:B3)/B3)','10','2','12','12','12'),
...
)
);
//方式二
//標題
$title_name = "月彙總出入境人數報表";
$objPHPExcel->getActiveSheet()->mergeCells('A1:J1');
$objPHPExcel->getActiveSheet()->SetCellValue('A1', $title_name);
$objPHPExcel->getActiveSheet()->SetCellValue('A2', '航次');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'E人數');
$objPHPExcel->getActiveSheet()->SetCellValue('C2', 'W人數');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', '小計');
$objPHPExcel->getActiveSheet()->SetCellValue('E2', '增加率');
$objPHPExcel->getActiveSheet()->SetCellValue('F2', 'E商人');
$objPHPExcel->getActiveSheet()->SetCellValue('G2', 'W商人');
$objPHPExcel->getActiveSheet()->SetCellValue('H2', '小計');
$objPHPExcel->getActiveSheet()->SetCellValue('I2', '團體');
$objPHPExcel->getActiveSheet()->SetCellValue('J2', '通常');
for($i=1;$i<=12;$i++){
$index = 'A'.($i+2);
$index_b = 'B'.($i+2);
$index_c = 'C'.($i+2);
$index_d = 'D'.($i+2);
$index_e = 'E'.($i+2);
$index_f = 'F'.($i+2);
$index_g = 'G'.($i+2);
$index_h = 'H'.($i+2);
$index_i = 'I'.($i+2);
$index_j = 'J'.($i+2);
$prev_index_d = 'D'.($i+1);
$growth_rate = $i == 1?'-':'=SUM(('.$index_d.'-'.$prev_index_d.')/'.$prev_index_d.')';
$objPHPExcel->getActiveSheet()->SetCellValue($index,'一月份');
$objPHPExcel->getActiveSheet()->SetCellValue($index_b, 12);
$objPHPExcel->getActiveSheet()->SetCellValue($index_c, 10);
$objPHPExcel->getActiveSheet()->SetCellValue($index_d, '=SUM('.$index_b.':'.$index_c.')');
$objPHPExcel->getActiveSheet()->SetCellValue($index_e, $growth_rate );
$objPHPExcel->getActiveSheet()->SetCellValue($index_f, 10);
$objPHPExcel->getActiveSheet()->SetCellValue($index_g, 20);
$objPHPExcel->getActiveSheet()->SetCellValue($index_h, '=SUM('.$index_f.':'.$index_g.')');
$objPHPExcel->getActiveSheet()->SetCellValue($index_i, 10);
$objPHPExcel->getActiveSheet()->SetCellValue($index_j, 10)
}
//開始圖表
//取得繪製圖表的標籤(圖例項)
$dataseriesLabels = array(
new \PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$2', NULL, 1), // '總人數'
new \PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$H$2', NULL, 1), // '商人'
new \PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$I$2', NULL, 1), // '團體'
new \PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$J$2', NULL, 1), // '通常旅客'
);
//取得圖表X軸的刻度 (第四個參數值:單元格長度A3-A14)
$xAxisTickValues = array(
new \PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$3:$A$14', NULL, 12), // 一月份 to 十二月份
);
//取得繪圖所需的數據
$dataSeriesValues = array(
new \PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$3:$D$14', NULL, 12),
new \PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$H$3:$H$14', NULL, 12),
new \PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$I$3:$I$14', NULL, 12),
new \PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$J$3:$J$14', NULL, 12),
);
//根據取得的東西作出一個圖表框架
$series = new \PHPExcel_Chart_DataSeries(
\PHPExcel_Chart_DataSeries::TYPE_SURFACECHART, // plotType
\PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
//設置生成圖表所須要的參數(座標是橫向或豎向顯示:DIRECTION_COL|DIRECTION_BAR)
$series->setPlotDirection(\PHPExcel_Chart_DataSeries::DIRECTION_COL);
$layout = new \PHPExcel_Chart_Layout();
$layout->setShowPercent(TRUE);
$plotarea = new \PHPExcel_Chart_PlotArea($layout, array($series));
$legend = new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new \PHPExcel_Chart_Title('2018年各渠道購票數對比圖');
// $xAxisLabel = new \PHPExcel_Chart_Title('Financial Period'); //X軸標題
$yAxisLabel = new \PHPExcel_Chart_Title('人數 ($k)'); //Y軸標題
$chart = new \PHPExcel_Chart(
'chart1', // name
$title, // title,空爲null
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
$yAxisLabel // yAxisLabel
);
//給定圖表所在表格中的位置
$chart->setTopLeftPosition('L2');
$chart->setBottomRightPosition('T15');
//將chart添加到sheet中
$objWorksheet->addChart($chart);
//--------------圖表end;
$objPHPExcel->getActiveSheet()->setTitle("整年合計");
$filename = '出入境人數報表.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE); //圖表必須
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=".$filename);
header("Content-Transfer-Encoding:binary");
$objWriter->save("php://output");
}post
已完結,暫未解決表格數據與圖表數據關聯。ui