首先得把phpexcel擴展的源碼拷貝到項目文件下

下面是代碼
/** 引入最重要的PHPExcel類庫的入口文件 */
require(STK_PATH.'/class/stk/PHPExcel.class.php'); require(STK_PATH.'/class/stk/PHPExcel/IOFactory.php'); $objPHPExcel = new PHPExcel(); $objSheet = $objPHPExcel->getActiveSheet();
//生成表格
$data = array( array("起始時間","2020/6/15 0:00:00","終止時間",'2020-06-15 17:21:45'), array("源IP",'0.0.0.0','目的IP','0.0.0.0'), array("目標標識","HTTP審計",'HTTPS審計','即時通訊審計','電子郵件審計','遠程登錄審計','文件傳輸審計'), array("條數",'10','256','3','100','20','46')//加空表示文本 0也顯示 );//這里面的數據可以用變量代替 $objSheet->fromArray($data);
//圖表屬性
$labels = array( new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$A$4',null,1),//條數圖例 ); $xLabels = array( new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$B$3:$G$3',null,6),//取x軸刻度 ); $datas = array( new PHPExcel_Chart_DataSeriesValues('Number','Worksheet!$B$4:$G$4',null,6),//取數據 ); $series = array( new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,//選擇圖表形式,柱狀圖,還可選擇其他 折線圖 餅圖 PHPExcel_Chart_DataSeries::GROUPING_STANDARD, range(0, count($datas)-1), $labels, $xLabels, $datas ) );
//圖表框架
$layout=new PHPExcel_Chart_Layout(); $layout->setShowVal(true); $areas = new PHPExcel_Chart_PlotArea($layout,$series); $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT,$layout,false); $title = new PHPExcel_Chart_Title("目標標識統計"); $ytitle = new PHPExcel_Chart_Title("條數"); $chart = new PHPExcel_Chart('line_chart',$title,$legend,$areas,true,false,null,$ytitle); $chart->setTopLeftPosition("A7")->setBottomRightPosition("K25"); //圖表位置 $objSheet->addChart($chart); $filename = '統計'.date('YmdHis').'.xlsx'; $excel = 'Excel2007'; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$excel); $objWriter->setIncludeCharts(true); //圖表必須加此行
//瀏覽器導出頭部
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //excel2007 header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0');
//解決Excel2007不能導出
$filePath = dirname(__FILE__) . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp"; $objWriter->save($filePath); readfile($filePath); unlink($filePath);
在選擇圖表形式的時候 可以選擇其他需要的圖表,然后只要相應的調整參數即可,

效果就是這樣
