http://www.jianshu.com/p/f6a7996b0410
PHPExcel的导入导出--导出
在上面链接的文章中,已经详细的介绍了PHPExcel导入导出,当然excel的样式也是最简单的那种。今天介绍的是比较高级的带有样式的excel表。基础的方法依然是使用上面链接里的。对导出还不是太理解的朋友,可以跟着我的思路再做一遍,let's go!
基本介绍:PHPCMF,PHPExcel类 simplewind/Core/Library/Vendor下面(第三方类库), 员工考勤统计表
1.PHPExcel类的方法重写
AdminbaseController.class.php
namespace Common\Controller;
use Common\Controller\AppframeController;
class AdminbaseController extends AppframeController {
/**
* @param $expTitle 名称
* @param $expCellName 参数
* @param $expTableData 内容
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
*/
public function exportExcelMonth($expTitle, $expTimes, $expCellName, $expTableData) {
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
$fileName = date('YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");
// $objPHPExcel = new \PHPExcel();
#你需要的样式的模板excel文件
$temPath = "./public/Huishuo/kaoqin.xlsx";
#检查文件路径
if (!file_exists($temPath)) {
$this->error('模板不存在');
return;
}
// if ($extension == 'xlsx') {
// $objReader = new PHPExcel_Reader_Excel2007();
// } else {
// $objReader = new PHPExcel_Reader_Excel5();
// }
//[注:原本是想根据模板类型选择不同的excel,后来发现只能使用2007,所以以下使用的都是2007]
//加载模板
$objPHPExcel = \PHPExcel_IOFactory::createReader("Excel2007")->load($temPath);
$objPHPExcel->getSheet(0)->setCellValue('A2', $expTimes);
$objPHPExcel->getActiveSheet(0)->getStyle('A2')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
foreach ($expCellName as $key => $v) {
$colum = chr($key + 65);
#样式设置
for ($j = 0; $j < $dataNum; $j++) {
$num = ($j + 5);
$objPHPExcel->getSheet(0)->setCellValue($colum . $num, $expTableData[$j][$expCellName[$key][0]]);
$color = $cellNum == $key + 1 ? 'ff9900' : '';
//模板中所能给出的行数样式是有限的(案例模板的样式在46行戛然而止),在众多数据的情况下,之后的行数需要我们自己添加每行的样式,由此,写了个方法进行调用
$this->setValue($objPHPExcel, $colum, $num, $color);
}
}
ob_end_clean(); //清除缓冲区,避免乱码
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $expTitle . '.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
public function setValue($objPHPExcel, $colum, $num, $color = '') {
if (!$color) {
$color = $num % 2 == 0 ? 'ffff99' : 'ffffcd';
}
#填充色
$objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getFill()->getStartColor()->setARGB($color);//背景色
$objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('008100');
$objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('008100');
// $objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('008100');
// $objPHPExcel->getActiveSheet()->getStyle($colum . $num)->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('008100');
// 边框色getTop(),getLeft(),getBottom(),getRight(),边框线类型setBorderStyle(),具体参数可以在phpExcel中进行查询
}
}
2.导出Excel
AdminMonthLogsController.class.php
namespace Huishuo\Controller;
use Huishuo\Controller\AdminHuishuoBaseController;
class AdminMonthLogsController extends AdminHuishuoBaseController {
/**
*
* 导出Excel
*/
public function expExcel() {
$xlsModel = $this->model;
$where = $this->where;
$xlsName = "综合月报统计" . date("Y-m-d H:i:s");
$xlsCell = array(
array('id', '序号'),
array('name', '职员名称'),
array('hours_total', '工作时数(标准)'),
array('hours_work', '工作时数(实际)'),
array('late', '迟到(次数)'),
array('late_times', '迟到(分钟)'),
array('back', '早退(次数)'),
array('back_times', '早退(分钟)'),
array('workdays', '出勤天数(标准/实际)'),
array('tasks_times', '任务时长'),
array('leaves_times', '请假时长'),
array('absenteeism', '旷职(天)'),
array('remark', '备注'),
);
$xlsData = $xlsModel->findall($where);
foreach ($xlsData as $key => $value) {
$xlsData[$key]['hours_total'] = $xlsData[$key]['hours_work'] = $xlsData[$key]['late_times'] = $xlsData[$key]['back_times'] = '';
$xlsData[$key]['workdays'] = $xlsData[$key]['totaldays'] . '天 / ' . $xlsData[$key]['workdays'] . ' 天';
$value['missing'] != 0 && $xlsData[$key]['remark'] = $value['missing'] . '次漏签';
}
$Start = date('Y/m/d', strtotime(I('get.start_time','2017-06') . '-01'));
#结束月份第一天
$BeginDate = I('get.end_time',date('Y-m')).'-01';
#结束月份最后一天
$EndDate = date('Y/m/d', strtotime("$BeginDate +1 month -1 day"));
$xlsTimes = '考勤日期 : ' .$Start. ' ~ ' . $EndDate . ' (**网络科技有限公司)';
$this->exportExcelMonth($xlsName, $xlsTimes, $xlsCell, $xlsData);
}
}
关于设置样式,学识并不渊博的我不可避免的借助了伟大的搜索引擎,借鉴了文档http://www.cnblogs.com/huangcong/p/3687665.html
到此,导出带有样式的excel表就完成了。我也是初试,写的并不是很好,也希望简友大神们能给出更好的建议。祝:共同进步