tp5.0操作phpexcel
1.首先在PHPExcel官网上面下载安装包下载地址为:https://github.com/PHPOffice/PHPExcel
2.将PHPExcel里的classes文件夹改名为PHPExcel,并且复制到tp5框架下的vendor文件内
3.在控制器中用PHPExcel,只需要
vendor(“PHPExcel.PHPExcel”);
即可。
4.例子如下:
//下面两个函数放入控制器中,直接使用即可
public function lst()
{
//此方法成功了,配合下面的public static function excelPut(expTableData){函数
//不使用compose,将phpexcel文件夹放入vendor
//读取sql数据
$res = array(
array(‘王城’, ‘男’, ‘18’, ‘1997-03-13’, ‘18948348924’),
array(‘李飞虹’, ‘男’, ‘21’, ‘1994-06-13’, ‘159481838924’),
array(‘王芸’, ‘女’, ‘18’, ‘1997-03-13’, ‘18648313924’),
array(‘郭瑞’, ‘男’, ‘17’, ‘1998-04-13’, ‘15543248924’),
array(‘李晓霞’, ‘女’, ‘19’, ‘1996-06-13’, ‘18748348924’),
);
//文件名称
$Excel['fileName']="安全表".date('Y年m月d日-His',time());//or $xlsTitle
$Excel['cellName']=['A','B','C','D','E','F','G','H','I','J'];
//$Excel['H'] = ['A'=>5,'B'=>12,'C'=>12,'D'=>12,'E'=>12,'F'=>12,'G'=>12,'H'=>12,'I'=>12,'J'=>12];//横向水平宽度
$Excel['V'] = ['1'=>40,'2'=>26];//纵向垂直高度
$Excel['sheetTitle']="安全清单";//大标题,自定义
//excel每列的名字
$Excel['xlsCell']=[ [0,'ID'],
[1,'岗位'],
[2,'部门'],
[3,'日期'],
[4,'事件时段']
];
//excel表单数据
$this->excelPut($Excel,$res);
}
//输出到Excel
//1.去网上下载PHPExcel,将classes文件重命名为PHPExcel
//2.将重命名后的classes文件夹(即PHPExcel)复制到tp5框架下的vendor文件下
//3.然后就可以使用vendor("PHPExcel.PHPExcel");
public static function excelPut(expTableData){
Excel[‘sheetTitle’]);
$cellName = $Excel[‘cellName’];
$xlsCell = $Excel[‘xlsCell’];
xlsCell);//计算总列数
expTableData);//计算数据总行数
vendor(“PHPExcel.PHPExcel”);
$objPHPExcel = new \PHPExcel();
$sheet0 = $objPHPExcel->getActiveSheet(0);
$sheet0->setTitle(“Sheet1”);//设置sheet名称
//设置表格标题A1
cellName[$cellNum-1].‘1’);//表头合并单元格
Excel[‘fileName’]);
// Excel[‘sheetTitle’].date(‘Y年m月d日’,time()));
$sheet0->getStyle(‘A1’)->getFont()->setSize(20);
Excel[‘H’])){
foreach ($Excel[‘H’] as $key => $value) {
key)->setWidth(KaTeX parse error: Expected 'EOF', got '}' at position 16: value);
}̲
}
//纵向…Excel[‘V’])){
foreach ($Excel[‘V’] as $key => $value) {
key)->setRowHeight($value);
}
}
//第二行:表头要加粗和居中,加入颜色
$sheet0->getStyle(‘A1’)
->applyFromArray([‘font’ => [‘bold’ => false],‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
$setcolor = cellName[$cellNum-1].“2”)->getFill();
$setcolor->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
// $colors=[‘00a000’,‘53a500’,‘3385FF’,‘00a0d0’,‘D07E0E’,‘c000c0’,‘0C8080’,‘EFE4B0’];//设置总颜色
// colors[mt_rand(0,count($colors)-1)];//获取随机颜色
// selectcolor);
i=0;cellNum;$i++){
cellName[$i].‘2’, i][1])
->getStyle(i].‘2’)
->applyFromArray([‘font’ => [‘bold’ => true],‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
}
//body:渲染表中数据内容部分
for(i<i++){
for(j<j++){
cellName[i+3))->applyFromArray([‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
cellName[i+3),i][j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
cellName[i+3))->getNumberFormat()->setFormatCode("@");
}
}
//设置边框
cellName[i+2))->applyFromArray([‘borders’ => [‘allborders’ => [‘style’ => \PHPExcel_Style_Border::BORDER_THIN]]]);
//dataNum+10)," “);//多设置一些行
header(‘pragma:public’);
header('Content-type:application/vnd.ms-excel;charset=utf-8;name=”’.Excel[‘fileName’].".xlsx");
//attachment新窗口打印inline本窗口打印
KaTeX parse error: Expected 'EOF', got '\PHPExcel' at position 13: objWriter = \̲P̲H̲P̲E̲x̲c̲e̲l̲_IOFactory::cre…objPHPExcel, ‘Excel2007’);
$objWriter->save(‘php://output’);
exit;
}
上一篇: 跟大家聊聊提升百度权重的几个方法
下一篇: spark 自定义Partitioner