直接上干货:
<?php
$cards_list = Cards::find($parameters);
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties()->setCreator("jiequan")
->setLastModifiedBy("jiequan")
->setTitle("card List")
->setSubject("card List")
->setDescription("card List")
->setKeywords("excel")
->setCategory("result file");
$exportKeys = ["number","password","gtime"];
/*以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/
$cell = ["A","B","C","D"];
$fieldsKeyValue = array(
"number" => "卡号",
"password" => "密码",
"gtime" => "领取时间",
);
foreach ($exportKeys as $k => $field) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell[$k] . '1', $fieldsKeyValue[$field]);
}
foreach ($cards_list as $k => $card) {
$num = $k + 1;
foreach ($exportKeys as $i => $exportKey) {
if($exportKey == "number" || $exportKey == "password") {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell[$i] . ($num + 1), $card->$exportKey,\PHPExcel_Cell_DataType::TYPE_STRING);
} else {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell[$i] . ($num + 1), $card->$exportKey);
}
}
$objPHPExcel->getActiveSheet()->getColumnDimension( 'A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension( 'B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension( 'C')->setWidth(20);
}
$objPHPExcel->getActiveSheet()->setTitle('card_List');
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$name = 'cardList' . "ex" . date("YmdHis",time()) . rand(1000,9999);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$name.'.xls"');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
php使用PHPExcel 导出数据表到Excel文件
abiao19812024-07-03 8:28
相关推荐
darkb1rd2 小时前
八、PHP SAPI与运行环境差异darkb1rd2 小时前
七、PHP配置(php.ini)安全最佳实践JSON_L3 小时前
Fastadmin中使用GatewayClient青茶3603 小时前
php怎么实现订单接口状态轮询请求wxin_VXbishe5 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790Risehuxyc7 小时前
备份三个PHP程序lpfasd1237 小时前
物联网后端岗位java面试题JSON_L7 小时前
Fastadmin中使用百度翻译APIm0_748229998 小时前
PHP简易聊天室开发指南darkb1rd8 小时前
六、PHP错误处理与异常机制