直接上干货:
<?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
相关推荐
狗凯之家源码网1 小时前
域名防红系统源码效果实测与能力边界解析2601_965798472 小时前
Why Most WordPress Themes Break Elementor and How I Fixed It郑州光合科技余经理8 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付Ivanqhz16 小时前
php8 use-def链DFT计算杂谈1 天前
ZrBr单层掺杂诱导的自旋/反常霍尔响应切换BingoGo1 天前
PHP 8.6 新特性一览90后小陈老师1 天前
从“打开就卡“到“秒开“:一次 PHP GD 动态海报生成的性能优化实战Sagittarius_A*1 天前
Burst Lab|PHP 审计 10:PHP 网络请求与 SSRF 漏洞审计思路JaguarJack1 天前
对标 npx 的 CPX PHP 的 Composer 包执行器BingoGo1 天前
对标 npx 的 CPX PHP 的 Composer 包执行器