php生成 excel

<?php
function exportExcel($data){
include ‘./PHPExcel/Classes/PHPExcel.php‘;
/*实例化excel类*/

$excel = new PHPExcel();

/*实例化excel图片处理类*/

$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(‘Customer_Name‘, ‘Customer_Email‘, ‘Product_Image‘, ‘Product_Name‘, ‘Product_Sku‘,‘Product_Describe‘,‘Product_Quantity‘,‘Order ID‘,‘Send_Adress‘);

/*填充表格表头*/

for($i = 0;$i < count($tableheader);$i++) {

$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");

}
/*设置表格数据*/


/*填充表格内容*/

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 == 2&&(substr($data[$i][$row],-3)=="jpg")) {

/*实例化插入图片类*/

$objDrawing = new PHPExcel_Worksheet_Drawing();

/*设置图片路径 切记:只能是本地图片*/

//$objDrawing->setPath("./no_image.jpg");
$objDrawing->setPath($data[$i][$row]);

/*设置图片高度*/

$objDrawing->setHeight(50);

/*设置图片要插入的单元格*/

$objDrawing->setCoordinates("$letter[$row]$j");

/*设置图片所在单元格的格式*/

$objDrawing->setOffsetX(80);

$objDrawing->setRotation(20);

$objDrawing->getShadow()->setVisible(true);

$objDrawing->getShadow()->setDirection(50);

$objDrawing->setWorksheet($excel->getActiveSheet());

continue;

}
$result = $data[$i][$row];
$excel->getActiveSheet()->setCellValue("$letter[$row]$j","$result");

}

}
/*实例化excel输入类并完成输出excel文件*/


if(isset($_GET[‘download‘])&&$_GET[‘download‘]=="download"){
$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=".time().".xls");

header("Content-Transfer-Encoding:binary");
$write->save(‘php://output‘);
}
}
$data = array(

array(‘小王‘, ‘男‘, ‘./no_image.jpg‘, ‘CS12‘, ‘qw‘,1,3,2,4,4),

array(‘小李‘, ‘女‘, ‘./no_image.jpg‘, ‘CS12‘, ‘qw‘),
array(‘小李‘, ‘女‘, ‘./no_image.jpg‘, ‘CS12‘, ‘qw‘),
array(‘小李‘, ‘女‘, ‘ww‘, ‘CS12‘, ‘qw‘)

);
exportExcel($data);
?>
<html>
<body>
<form action="./index.php" method="get">
<input name="download" value="download" type="submit"/>
</form>
</body>
</html>

php生成 excel

上一篇:Centos610安装DotnetCore3.1


下一篇:《大型网站技术架构演进与性能优化》——第六章: