编程语言
首页 > 编程语言> > 基于ThinkPHP开发的PHPExcel导出

基于ThinkPHP开发的PHPExcel导出

作者:互联网

首先我们需要去PHPExcel官网下载开源包

下载好了以后只需要把里面的classes文件夹解压出来就可以用了!

当然,在处理的数据量庞大的时候就会报错,提示你看nginx日志什么的,这个时候你需要检查php.ini中的memory_limit,如果是128什么的比较小的数字就改大一点,改成512,就可以解决了~

在ThinkPHP中,文件的命名空间还是很重要的,我试着按照其他的第三方命名写namespace但是还是不行,最后我是这么做的

第一步:重命名classes为Excel,并且将文件夹内的PHPExcel.php重命名成PHPExcel.class.php

然后放入到目录文件

 

 

然后在控制层导入,直接use的话是会报错找不到类文件的。。。也许是内核里面的路由问题,于是我是在实例化的时候写的详细地址居然可以出来了

$objPHPExcel = new \Org\Excel\PHPExcel();   就是这样

但是这样类文件的一些方法是不可以用的,如果调用需要在方法前面加一个\

比如这样:
$objSheet->getStyle("A1:Z1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('#00FF00');//给单元格填充背景颜色


如下是具体代码:

//excel导出
public function excel(){

//查询sql生成数组
$loan = M("loan");
$data = $loan->select();

$objPHPExcel = new \Org\Excel\PHPExcel();//实例化类文件 等同于在桌面新建一个excel表格
$objSheet = $objPHPExcel->getActiveSheet();//获得当前活动sheet操作对象
$objSheet->setTitle('loan_info');//给当前活动sheet修改名称

$objSheet->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置单元格垂直居中、水平居中
$objSheet->getStyle("A1:Z1")->getFont()->setName("微软雅黑")->setSize(10)->setBold(true);//设置单元格范围的字体、字体大小、加粗
$objSheet->getStyle("A1:Z1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('#00FF00');//给单元格填充背景颜色

//添加边框
$styleArray = array(
'borders' => array(
'outline' => array(
'style' => \PHPExcel_Style_Border::BORDER_THICK,
'color' => array('argb' => '#F0F8FF'),
),
),
);
$objSheet->getStyle("A1")->applyFromArray($styleArray);
$objSheet->getStyle("B1")->applyFromArray($styleArray);
$objSheet->getStyle("C1")->applyFromArray($styleArray);
$objSheet->getStyle("D1")->applyFromArray($styleArray);
$objSheet->getStyle("E1")->applyFromArray($styleArray);
$objSheet->getStyle("F1")->applyFromArray($styleArray);
$objSheet->getStyle("G1")->applyFromArray($styleArray);
$objSheet->getStyle("H1")->applyFromArray($styleArray);
$objSheet->getStyle("I1")->applyFromArray($styleArray);
$objSheet->getStyle("J1")->applyFromArray($styleArray);
$objSheet->getStyle("K1")->applyFromArray($styleArray);
$objSheet->getStyle("L1")->applyFromArray($styleArray);
$objSheet->getStyle("M1")->applyFromArray($styleArray);
$objSheet->getStyle("N1")->applyFromArray($styleArray);
$objSheet->getStyle("O1")->applyFromArray($styleArray);
$objSheet->getStyle("P1")->applyFromArray($styleArray);
$objSheet->getStyle("Q1")->applyFromArray($styleArray);
$objSheet->getStyle("R1")->applyFromArray($styleArray);
$objSheet->getStyle("S1")->applyFromArray($styleArray);
$objSheet->getStyle("T1")->applyFromArray($styleArray);
$objSheet->getStyle("U1")->applyFromArray($styleArray);
$objSheet->getStyle("V1")->applyFromArray($styleArray);
$objSheet->getStyle("W1")->applyFromArray($styleArray);



$objSheet->setCellValue("A1","ID")
->setCellValue("B1","公司名称")
->setCellValue("C1","公司LOGO")
->setCellValue("D1","链接地址")
->setCellValue("E1","贷款对应人群")
->setCellValue("F1","宣传语")
->setCellValue("G1","最小额度范围")
->setCellValue("H1","最大额度范围")
->setCellValue("I1","最小期限范围")
->setCellValue("J1","最大期限范围")
->setCellValue("K1","利率")
->setCellValue("L1","最快放款时间")
->setCellValue("M1","计息方式")
->setCellValue("N1","是否认证")
->setCellValue("O1","申请条件")
->setCellValue("P1","申请流程")
->setCellValue("Q1","申请所需材料")
->setCellValue("R1","申请时间")
->setCellValue("S1","更新时间")
->setCellValue("T1","成功贷款人数")
->setCellValue("U1","按点击收费金额")
->setCellValue("V1","是否上架")
->setCellValue("W1","显示序号排列");//给单元格填充相应的值

//循环填充
$j = 2;
foreach($data as $key => $val){
$objSheet->setCellValue("A".$j,$val['id'])
->setCellValue("B".$j,$val['corporate'])
->setCellValue("C".$j,$val['img'])
->setCellValue("D".$j,$val['corporate_url'])
->setCellValue("E".$j,$val['person'])
->setCellValue("F".$j,$val['propaganda'])
->setCellValue("G".$j,$val['xquota'])
->setCellValue("H".$j,$val['dquota'])
->setCellValue("I".$j,$val['xterm'])
->setCellValue("J".$j,$val['dterm'])
->setCellValue("K".$j,$val['rate'])
->setCellValue("L".$j,$val['qtime'])
->setCellValue("M".$j,$val['type'])
->setCellValue("N".$j,$val['authentication'])
->setCellValue("O".$j,$val['condition'])
->setCellValue("P".$j,$val['process'])
->setCellValue("Q".$j,$val['science'])
->setCellValueExplicit("R".$j,$val['apply_time'],\PHPExcel_Cell_DataType::TYPE_STRING)//设定日期固定的为数字合适不会成为官方看不懂的格式
->setCellValueExplicit("S".$j,$val['up_time'],\PHPExcel_Cell_DataType::TYPE_STRING)//设定日期固定的为数字合适不会成为官方看不懂的格式
->setCellValue("T".$j,$val['succ_person'])
->setCellValue("U".$j,$val['click_money'])
->setCellValue("V".$j,$val['is_shelves'])
->setCellValue("W".$j,$val['show_number']);
$j++;
}

//设置文件保存的命名、编码、以及开放保存路径权限
$fn= "loan_".time().".xls";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Disposition: attachment;filename=$fn");//告诉浏览器将要输出的名称
header('Cache-Control: max-age=0');//禁止缓存
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件
$objWriter->save('php://output');//彻底开放保存路径
exit;
}

标签:applyFromArray,val,styleArray,导出,objSheet,getStyle,ThinkPHP,PHPExcel,setCellValu
来源: https://www.cnblogs.com/findher/p/10610310.html