简易的phpexcel导出柱状图
首先得把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);
在选择图表形式的时候 可以选择其他需要的图表,然后只要相应的调整参数即可,
效果就是这样