·设为首页收藏本站📧邮箱修改🎁免费下载专区💎积分✅卡密📒收藏夹👽聊天室
返回列表 发布新帖

PHPExcel生成柱状图

198 0
发表于 2024-2-10 18:43:12 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
PHPExcel生成柱状图 filename,array,newPHPExcel,chart,柱状图

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser');

import('ORG.phpexcel.PHPExcel', '', '.php');
import('ORG.phpexcel.PHPExcel.IOFactory', '', '.php');
import('ORG.phpexcel.PHPExcel.Chart.Title', '', '.php');


//////////////////////////////////////
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('标题标题标题标题',   121000),
array('选项1',   121),
array('选项2',   566),
array('选项3',   529),
array('选项4',   30),
)
);
$objWorksheet->setTitle("Q2"); //给当前sheet设置名称



$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),//2010
//new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),//2011
//new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),//2012
);

$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),//Q1 to Q4
);

$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
//new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
//new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
);

//Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART,// plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,// plotGrouping
range(0, count($dataSeriesValues)-1),// plotOrder
$dataseriesLabels,// plotLabel
$xAxisTickValues,// plotCategory
$dataSeriesValues// plotValues
);
//Set additional dataseries parameters
//Make it a horizontal bar rather than a vertical column graph
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);

//Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
//Set the chart legend
//$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$legend  = NULL;
$title   = new PHPExcel_Chart_Title('测试题1');
$yAxisLabel = new PHPExcel_Chart_Title('参与人数');


//Create the chart
$chart = new PHPExcel_Chart(
'chart1',// name
$title,// title
$legend,// legend
$plotarea,// plotArea
true,// plotVisibleOnly
0,// displayBlanksAs
NULL,// xAxisLabel
$yAxisLabel// yAxisLabel
);

//Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');

//Add the chart to the worksheet
$objWorksheet->addChart($chart);
//////////////////////////////////////////////////////////////////////////////////////

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);


$filename = '明细表_'.date("Y_m_d").".xlsx";

// Redirect output to a client’s web browser (Excel2007)
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//
//////////////////////////////////////////
//处理中文文件名乱码问题
$ua = $_SERVER["HTTP_USER_AGENT"];  
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20",$encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
    $filename = $encoded_filename;
    header('Content-Disposition: attachment;filename="' .$filename . '"');
}else if (preg_match("/Firefox/", $ua)){
   header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
}else {
  header('Content-Disposition: attachment; filename="' . $filename . '"');
}
////////////////////////////////////////

header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter->save('php://output');
exit;

红色的数据部分,多加几列就变成多柱状图,并且后面红色部分也要相应的变更。
其它相关链接:
http://blog.csdn.net/qq_20480611/article/details/47982501
http://blog.csdn.net/qq_20480611/article/details/48000007
http://blog.csdn.net/xcl168/article/details/25001205
http://www.cnblogs.com/phpgcs/p/phpexcel_chart_2.html?utm_source=tuicool&utm_medium=referral
http://blog.csdn.net/ouyangyiqin/article/details/8901038
http://www.tuicool.com/articles/vA7VJj
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

创宇盾启航版免费网站防御网站加速服务
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:3.143.237.54,GMT+8, 2024-12-22 18:05 , Processed in 0.205276 second(s), 79 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2024 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表