直接上干货:
<?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
相关推荐
hopetomorrow4 小时前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决网络安全-杰克4 小时前
网络安全概论不是二师兄的八戒4 小时前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启黑客Ela5 小时前
网络安全中常用浏览器插件、拓展蝶开三月7 小时前
php:使用socket函数创建WebSocket服务Ai 编码助手8 小时前
使用php和Xunsearch提升音乐网站的歌曲搜索效果wkj00110 小时前
php操作redis黑客Ash19 小时前
【D01】网络安全概论->yjy19 小时前
计算机网络(第一章)阳光帅气男孩20 小时前
PhpSpreadsheet导出图片