欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tp5.0操作phpexcel

程序员文章站 2022-06-14 08:11:54
...

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(Excel,Excel,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(Excel,Excel,expTableData){
Excel[sheetTitle]=iconv(utf8,gb2312,Excel['sheetTitle']=iconv('utf-8', 'gb2312',Excel[‘sheetTitle’]);
$cellName = $Excel[‘cellName’];
$xlsCell = $Excel[‘xlsCell’];
cellNum=count(cellNum = count(xlsCell);//计算总列数
dataNum=count(dataNum = count(expTableData);//计算数据总行数
vendor(“PHPExcel.PHPExcel”);
$objPHPExcel = new \PHPExcel();
$sheet0 = $objPHPExcel->getActiveSheet(0);
$sheet0->setTitle(“Sheet1”);//设置sheet名称
//设置表格标题A1
sheet0>mergeCells(A1:.sheet0->mergeCells('A1:'.cellName[$cellNum-1].‘1’);//表头合并单元格
objPHPExcel>setActiveSheetIndex(0)>setCellValue(A1,objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1',Excel[‘fileName’]);
// objPHPExcel>setActiveSheetIndex(0)//>setCellValue(A1,objPHPExcel->setActiveSheetIndex(0) // ->setCellValue('A1',Excel[‘sheetTitle’].date(‘Y年m月d日’,time()));
$sheet0->getStyle(‘A1’)->getFont()->setSize(20);
sheet0>getStyle(A1)>getFont()>setName();////if(isset(sheet0->getStyle('A1')->getFont()->setName('微软雅黑'); //设置行高和列宽 //横向水平宽度 if(isset(Excel[‘H’])){
foreach ($Excel[‘H’] as $key => $value) {
sheet0>getColumnDimension(sheet0->getColumnDimension(key)->setWidth(KaTeX parse error: Expected 'EOF', got '}' at position 16: value); }̲ } //纵向…Excel[‘V’])){
foreach ($Excel[‘V’] as $key => $value) {
sheet0>getRowDimension(sheet0->getRowDimension(key)->setRowHeight($value);
}
}
//第二行:表头要加粗和居中,加入颜色
$sheet0->getStyle(‘A1’)
->applyFromArray([‘font’ => [‘bold’ => false],‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
$setcolor = sheet0>getStyle("A2:".sheet0->getStyle("A2:".cellName[$cellNum-1].“2”)->getFill();
$setcolor->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
// $colors=[‘00a000’,‘53a500’,‘3385FF’,‘00a0d0’,‘D07E0E’,‘c000c0’,‘0C8080’,‘EFE4B0’];//设置总颜色
// selectcolor=selectcolor=colors[mt_rand(0,count($colors)-1)];//获取随机颜色
// setcolor>getStartColor()>setRGB(setcolor->getStartColor()->setRGB(selectcolor);
setcolor>getStartColor()>setRGB(A6A6A6);//for(setcolor->getStartColor()->setRGB('A6A6A6'); //根据表格数据设置列名称 for(i=0;i<i<cellNum;$i++){
objPHPExcel>setActiveSheetIndex(0)>setCellValue(objPHPExcel->setActiveSheetIndex(0) ->setCellValue(cellName[$i].‘2’, xlsCell[xlsCell[i][1])
->getStyle(cellName[cellName[i].‘2’)
->applyFromArray([‘font’ => [‘bold’ => true],‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
}
//body:渲染表中数据内容部分
for(i=0;i=0;i<dataNum;dataNum;i++){
for(j=0;j=0;j<cellNum;cellNum;j++){
sheet0&gt;getStyle(sheet0-&gt;getStyle(cellName[j].(j].(i+3))->applyFromArray([‘alignment’ => [‘horizontal’ => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,‘vertical’=>\PHPExcel_Style_Alignment::VERTICAL_CENTER]]);
sheet0&gt;setCellValueExplicit(sheet0-&gt;setCellValueExplicit(cellName[j].(j].(i+3),expTableData[expTableData[i][xlsCell[xlsCell[j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
sheet0&gt;getStyle(sheet0-&gt;getStyle(cellName[j].(j].(i+3))->getNumberFormat()->setFormatCode("@");
}
}
//设置边框
sheet0&gt;getStyle(A2:.sheet0-&gt;getStyle(&#x27;A2:&#x27;.cellName[cellNum1].(cellNum-1].(i+2))->applyFromArray([‘borders’ => [‘allborders’ => [‘style’ => \PHPExcel_Style_Border::BORDER_THIN]]]);
//sheet0&gt;setCellValue(&quot;A&quot;.(sheet0-&gt;setCellValue(&quot;A&quot;.(dataNum+10)," “);//多设置一些行
header(‘pragma:public’);
header('Content-type:application/vnd.ms-excel;charset=utf-8;name=”’.Excel[sheetTitle]..xlsx&quot;);header(&quot;ContentDisposition:attachment;filename=&quot;.Excel[&#x27;sheetTitle&#x27;].&#x27;.xlsx&quot;&#x27;); header(&quot;Content-Disposition:attachment;filename=&quot;.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;
}

相关标签: phpexcel