php Excel 导出图片,导出统计图

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
$dir = dirname(__FILE__);
require $dir . "/PHPExcel-1.8/Classes/PHPExcel.php";
// 导出图片
$excel = new PHPExcel();
$objDrawing = new PHPExcel_Worksheet_Drawing();
/*设置文本对齐方式*/
$excel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet = $excel->getActiveSheet();
$letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N');
/*设置表头数据*/
$tableheader = array('姓名', '性别', '年龄', '班级', '头像');
/*填充表格表头*/
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
/*设置表格数据*/
$data = array(
array('小王', '男', '20', 'CS12', 'test.jpg'),
array('小李', '女', '21', 'CS12', 'test.jpg'),
array('小周', '男', '22', 'CS12', 'test.jpg'),
array('小赵', '女', '23', 'CS12', 'test.jpg'),
array('小张', '男', '24', 'CS12', 'test.jpg')
);
/*填充表格内容*/
for ($i = 0;$i < count($data);$i++) {
$j = $i + 2;
/*设置表格宽度*/
$objActSheet->getColumnDimension("$letter[$i]")->setWidth(20);
/*设置表格高度*/
$excel->getActiveSheet()->getRowDimension($j)->setRowHeight(100);
/*向每行单元格插入数据*/
for ($row = 0;$row < count($data[$i]);$row++) {
if ($row == (count($data[$i]) -1 )) {
/*实例化插入图片类*/
$objDrawing = new PHPExcel_Worksheet_Drawing();
/*设置图片路径 全地址*/
$objDrawing->setPath('F:\PHPTutorial\WWW\execl_dctp\1569838450.png');
/*设置图片高度*/
$objDrawing->setHeight(125);
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates("$letter[$row]$j");
/*设置图片所在单元格的格式*/
$objDrawing->setOffsetX(1);
$objDrawing->setOffsetY(1);
$objDrawing->setRotation(1);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(10);
$objDrawing->setWorksheet($excel->getActiveSheet());
continue;
}
$excel->getActiveSheet()->setCellValue("$letter[$row]$j","111");
}
}
$write = new PHPExcel_Writer_Excel5($excel);
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="测试文件.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
//导出条形图
//$object = new PHPExcel();
//$objSheet = $object->getActiveSheet();
////生成表格
//$array = array(
//    array("", "实到", "应到", "率",),
//    array("龙华(50%)", 20, 30, 50),
//    array("龙华(40%)", 30, 50, 40),
//    array("龙华(60%)", 15, 17, 60),
//
//    array("龙华(60%)", 15, 17, 60),
//    array("龙华(60%)", 15, 17, 60),
//    array("龙华(60%)", 15, 17, 60)
//);
//$objSheet->fromArray($array);
//
////取出每个班级成绩的所在列
//$dataseriesLabels = array(
//    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),    //    一班
//    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),    //    二班
//    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),    //    三班
//);
//
////不及格、良好、优秀所在单元格
//$xAxisTickValues = array(
//    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$8', NULL, 5),    //    A2 to A5
//);
//
////取出每个班级成绩数据的所在列
//$dataSeriesValues = array(
//    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$8', NULL, 5),
//    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$8', NULL, 5),
//    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$8', NULL, 5),
//);
//
//$series = new PHPExcel_Chart_DataSeries(
//    PHPExcel_Chart_DataSeries::TYPE_BARCHART,        // 条线图
//    // PHPExcel_Chart_DataSeries::GROUPING_STACKED,    // plotGrouping
//    PHPExcel_Chart_DataSeries::GROUPING_STANDARD,    // plotGrouping
//    range(0, count($dataSeriesValues)-1),            // plotOrder
//    $dataseriesLabels,                                // plotLabel
//    $xAxisTickValues,                                // plotCategory
//    $dataSeriesValues                                // plotValues
//);
//
////    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_TOPRIGHT, NULL, false);
//
//$title = new PHPExcel_Chart_Title('XX小区督导图XX日XX天XX日');
//$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
//
////    Create the chart
//$chart = new PHPExcel_Chart(
//    'chart1',        // name
//    $title,            // title
//    $legend,        // legend
//    $plotarea,        // plotArea
//    true,            // plotVisibleOnly
//    0,                // displayBlanksAs
//    NULL,            // xAxisLabel
//    $yAxisLabel        // yAxisLabel
//);
//
////设置图形的所在区域
//$chart->setTopLeftPosition('A7');
//$chart->setBottomRightPosition('X30');
//
//$objSheet->addChart($chart);
//
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Content-Disposition: attachment;filename="03simple.xlsx"');
//header('Cache-Control: max-age=0');
//
//$objWriter = PHPExcel_IOFactory::createWriter($object, 'Excel2007');
//$objWriter->setIncludeCharts(TRUE);
//$objWriter->save('php://output');
function p($arr = ''){
echo "<pre>";
print_r($arr);
die;
}
?>
点赞

发表评论