php中excel带下拉框
dio 2023-05-13 php
# php导出excel带下拉框
<?php
namespace App\Lib;
use App\Models\CityModel;
class UserExcel
{
/**
* Excel句柄
* @var
*/
private $objExcel;
/**
* sheet和文件名
* @var
*/
private $title;
public function getFieldData()
{
$fieldData = [
[
'pColumn' => 'A',
'pValue' => '员工ID',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'B',
'pValue' => '姓名',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'C',
'pValue' => '手机号',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'D',
'pValue' => '主部门',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'E',
'pValue' => '邮件',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'F',
'pValue' => '职位',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'G',
'pValue' => '职务',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ["普通员工", "管理岗"],
],
[
'pColumn' => 'H',
'pValue' => '工号',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'I',
'pValue' => '花名',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'J',
'pValue' => '员工类型',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['全职', '兼职', '实习', '劳务派遣', '退休返聘', '劳务外包'],
],
[
'pColumn' => 'K',
'pValue' => '入职日期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'L',
'pValue' => '试用期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['无试用期', '一个月', '二个月', '三个月', '六个月']
],
[
'pColumn' => 'M',
'pValue' => '转正日期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'N',
'pValue' => '证件号码',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'O',
'pValue' => '出生日期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'P',
'pValue' => '性别',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['男', '女']
],
[
'pColumn' => 'Q',
'pValue' => '民族',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'R',
'pValue' => '籍贯',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => CityModel::select('region_name')
->whereIn('level', [1])
->pluck('region_name')->toArray(),
],
[
'pColumn' => 'S',
'pValue' => '户籍类型',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['本地城镇', '本地农村', '外地城镇(省内)', '外地农村(省内)', '外地城镇(省外)', '外地农村(省外)']
],
[
'pColumn' => 'T',
'pValue' => '户口所在地',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'U',
'pValue' => '现居住地址',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'V',
'pValue' => '婚姻状况',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['未婚', '已婚', '离异', '丧偶', '分居'],
],
[
'pColumn' => 'W',
'pValue' => '政治面貌',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['团员', '党员', '群众', '其他'],
],
[
'pColumn' => 'X',
'pValue' => '社保账号',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'Y',
'pValue' => '公积金账号',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'Z',
'pValue' => '学历',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['小学', '初中', '高中', '中专', '大专', '本科', '硕士', '博士', '其他'],
],
[
'pColumn' => 'AA',
'pValue' => '毕业学校',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AB',
'pValue' => '毕业时间',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AC',
'pValue' => '所学专业',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AD',
'pValue' => '银行卡号',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AE',
'pValue' => '开户行',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AF',
'pValue' => '最新合同公司',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AG',
'pValue' => '最新合同类型',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'select' => ['固定期限劳动合同', '无固定期限劳动合同', '实习协议', '劳务协议', '劳务派遣合同', '返聘协议', '短期劳动合同', '其他'],
],
[
'pColumn' => 'AH',
'pValue' => '最新合同起始日期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AI',
'pValue' => '最新合同到期日期',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AJ',
'pValue' => '续签次数',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AK',
'pValue' => '紧急联系人',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
[
'pColumn' => 'AL',
'pValue' => '联系人关系',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
'select' => ['父母', '配偶', '子女', '其他']
],
[
'pColumn' => 'AM',
'pValue' => '联系人电话',
'width' => 20,
'dateType' => \PHPExcel_Cell_DataType::TYPE_STRING,
'dateFormat' => "",
],
];
return $fieldData;
}
public function export($data = [], $header = [], $title = "花名册")
{
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS");
header("Access-Control-Allow-Headers:X-Requested-With,authorization,Content-Type,Accept,Origin,Token");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
$fieldData = $this->getFieldData();
$dataInfo = $data;
ini_set('memory_limit', '4096M');
$this->title = $title;
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize' => '15MB');
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$this->objExcel = new \PHPExcel();
$objSheet = $this->objExcel->getActiveSheet();
// $this->setHeader($objSheet, $fieldData);
//设置表头start
//设置sheet名
$objSheet->setTitle($this->title);
//所有单元格横向居中
$this->objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//所有单元格纵向居中
$this->objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置表头,行宽
$pCoordinate = '';
$isFontColor = false;
foreach ($fieldData as $fieldDatum) {
$pCoordinate = $fieldDatum['pColumn'] . '1';
$objSheet->setCellValue($pCoordinate, $fieldDatum['pValue']);
//设置行宽
$objSheet->getColumnDimension($fieldDatum['pColumn'])->setWidth($fieldDatum['width'] ?? 20);
if (!empty($fieldDatum['comment'])) {
$objSheet->getComment($pCoordinate)->getText()->createTextRun($fieldDatum['comment']);
}
if (!empty($fieldDatum['fontColor'])) {
$objSheet->getStyle($pCoordinate)->getFont()->getColor()->setRGB($fieldDatum['fontColor']);
$isFontColor = true;
}
if (!empty($fieldDatum['dateFormat'])) {
$objSheet->getStyle($fieldDatum['pColumn'])->getNumberFormat()->setFormatCode($fieldDatum['dateFormat']);
}
/*设置下拉*/
if (!empty($fieldDatum['select'])) {
$selectStr = implode(',', $fieldDatum['select']);
// foreach ($fieldDatum['select'] as $key => $val) {
//设定1000的下拉框
$n = 0;
$num = 1000;
// 我这里设置1000行,可自行设置
while($n < $num) {
$objSheet->getCell($fieldDatum['pColumn'] . ($n + 2))->getDataValidation()
->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
->setAllowBlank(false)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内(请不要自己填写,应在下拉列表中选择)')
->setPromptTitle()
->setPrompt()
->setFormula1('"' . $selectStr . '"');
$n++;
}
// }
}
}
if (!$isFontColor) {
//表头字体颜色
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->getColor()->setRGB('FFFFFF');
//表头背景颜色
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('595959');
}
//设置表头字体大小和加粗
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->setSize(11)->setBold(true);
//表头填充样式
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
//设置表头end
// $this->setData($objSheet, $fieldData, $dataInfo);
//数据填充start
if (empty($dataInfo)) {
return;
}
//没给表头对应字段
$kv = [
"A" => "staffId",
"B" => "staffName",
"C" => "mobile",
"D" => "dept",
"E" => "email",
"F" => "positionName",
"G" => "postName",
"H" => "staffCode",
"I" => "roster",
"J" => "staffType",
"K" => "joinDate",
"L" => "probation",
"M" => "turnDate",
"N" => "idCard",
"O" => "birthday",
"P" => "gender",
"Q" => "nation",
"R" => "placeOfOrigin",
"S" => "householdRegisterType",
"T" => "householdLocation",
"U" => "curResidence",
"V" => "maritalStatus",
"W" => "politicsFace",
"X" => "socialAccount",
"Y" => "providentFundAccount",
"Z" => "education",
"AA" => "graduateSchool",
"AB" => "graduateDate",
"AC" => "major",
"AD" => "bankCardNum",
"AE" => "bankName",
"AF" => "contractName",
"AG" => "contractType",
"AH" => "contractStartAt",
"AI" => "contractEndAt",
"AJ" => "renewalNum",
"AK" => "contact",//紧急联系人
"AL" => "relation",//紧急联系人关系
"AM" => "contactMobile",//紧急联系人电话
];
foreach ($dataInfo as $key => $value) {
$key += 2;
foreach ($fieldData as $fieldKey => $fieldDatum) {
if (!empty($fieldDatum['dateType'])) {
$objSheet->setCellValueExplicit($fieldDatum['pColumn'] . $key, $value[$kv[$fieldDatum['pColumn']]], $fieldDatum['dateType']);
continue;
}
$objSheet->setCellValue($fieldDatum['pColumn'] . $key, $value[$kv[$fieldDatum['pColumn']]]);
}
}
//数据填充end
//导出execl
ob_end_clean();//清除缓冲区,避免乱码
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Content-Type:application/force-download');
header("Content-Type:application/vnd.ms-excel");
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header("Content-Disposition:" . $this->downloadHeaders($title . ".xlsx"));
header('Content-Transfer-Encoding:binary');
$objWriter = \PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
/**
* 返回工作表操作句柄
* @return PHPExcel_Worksheet 工作表操作句柄
* @throws PHPExcel_Exception
*/
protected function getHandle(): PHPExcel_Worksheet
{
$this->objExcel = new \PHPExcel();
return $this->objExcel->getActiveSheet();
}
/**
* 设置表头
* @param PHPExcel_Worksheet $objSheet 工作表句柄
* @param array $fieldData 表头字段及格式
* @throws PHPExcel_Exception
*/
protected function setHeader(PHPExcel_Worksheet $objSheet, array $fieldData)
{
//设置sheet名
$objSheet->setTitle($this->title);
//所有单元格横向居中
$this->objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//所有单元格纵向居中
$this->objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置表头,行宽
$pCoordinate = '';
$isFontColor = false;
foreach ($fieldData as $fieldDatum) {
$pCoordinate = $fieldDatum['pColumn'] . '1';
$objSheet->setCellValue($pCoordinate, $fieldDatum['pValue']);
//设置行宽
$objSheet->getColumnDimension($fieldDatum['pColumn'])->setWidth($fieldDatum['width'] ?? 20);
if (!empty($fieldDatum['comment'])) {
$objSheet->getComment($pCoordinate)->getText()->createTextRun($fieldDatum['comment']);
}
if (!empty($fieldDatum['fontColor'])) {
$objSheet->getStyle($pCoordinate)->getFont()->getColor()->setRGB($fieldDatum['fontColor']);
$isFontColor = true;
}
if (!empty($fieldDatum['dateFormat'])) {
$objSheet->getStyle($fieldDatum['pColumn'])->getNumberFormat()->setFormatCode($fieldDatum['dateFormat']);
}
/*设置下拉*/
if (!empty($fieldDatum['select'])) {
$selectStr = implode(',', $fieldDatum['select']);
foreach ($fieldDatum['select'] as $key => $val) {
$objSheet->getCell($fieldDatum['pColumn'] . ($key + 2))->getDataValidation()
->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
->setAllowBlank(false)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内(请不要自己填写,应在下拉列表中选择)')
->setPromptTitle()
->setPrompt()
->setFormula1('"' . $selectStr . '"');
}
}
}
if (!$isFontColor) {
//表头字体颜色
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->getColor()->setRGB('FFFFFF');
//表头背景颜色
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('595959');
}
//设置表头字体大小和加粗
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->setSize(11)->setBold(true);
//表头填充样式
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
}
/**
* 设置数据
* @param PHPExcel_Worksheet $objSheet 工作表句柄
* @param array $fieldData 表头字段及格式
* @param array $dataInfo 数据信息
*/
protected function setData(PHPExcel_Worksheet $objSheet, array $fieldData, array $dataInfo): void
{
if (empty($dataInfo)) {
return;
}
foreach ($dataInfo as $key => $value) {
$key += 2;
foreach ($fieldData as $fieldKey => $fieldDatum) {
if (!empty($fieldDatum['dateType'])) {
$objSheet->setCellValueExplicit($fieldDatum['pColumn'] . $key, $value[$fieldKey], $fieldDatum['dateType']);
continue;
}
$objSheet->setCellValue($fieldDatum['pColumn'] . $key, $value[$fieldKey]);
}
}
}
public function export12($data = [], $header = [], $title = "花名册")
{
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS");
header("Access-Control-Allow-Headers:X-Requested-With,authorization,Content-Type,Accept,Origin,Token");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
// $header = [
// '单品名称', '单品编码', '单品规格', '单品单位',
// '网购地址', '单品类别', '固定资产', '专属机构',
// '供应商', '总部成本', '校区成本', '供应商报价',
// '学员价', '普通价', '小店成本价', '最低转卖价',
// ];//表头及字段名
if (empty($header)) return ["error" => false, "message" => "表头数据不能为空!!"];
// $branch = M('branch')->where(['branch_name' => ['NEQ', '']])->getField('branch_name', true);
// $supplier = M('supplier')->where(['status' => 1, 'supplier_name' => ['NEQ', '']])->getField('supplier_name', true);
// $goods_type = M('goods_type')->where(['pid' => 0])->getField('type_name', true);
$spectials = [
['column' => 'G', 'select_options' => ["普通员工", "管理岗"]],//职务
['column' => 'J', 'select_options' => ['全职', '兼职', '实习', '劳务派遣', '退休返聘', '劳务外包']],//员工类型
['column' => 'L', 'select_options' => ['无试用期', '一个月', '二个月', '三个月', '六个月']],//试用期
['column' => 'P', 'select_options' => ['男', '女']],//性别
['column' => 'S', 'select_options' => ['本地城镇', '本地农村', '外地城镇(省内)', '外地农村(省内)', '外地城镇(省外)', '外地农村(省外)']],//户籍类型
['column' => 'V', 'select_options' => ['未婚', '已婚', '离异', '丧偶', '分居']],//婚姻状况
['column' => 'W', 'select_options' => ['团员', '党员', '群众', '其他']],//政治面貌
['column' => 'Z', 'select_options' => ['小学', '初中', '高中', '中专', '大专', '本科', '硕士', '博士', '其他']],//学历
['column' => 'AG', 'select_options' => ['固定期限劳动合同', '无固定期限劳动合同', '实习协议', '劳务协议', '劳务派遣合同', '返聘协议', '短期劳动合同', '其他']],//最新合同类型
['column' => 'AL', 'select_options' => ['父母', '配偶', '子女', '其他']],//紧急联系人
];
//引入核心文件
$objPHPExcel = new \PHPExcel();
//定义配置
$topNumber = 1;//表头有几行占用
$xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称
$fileName = $title . date('_Ymd');//文件名称
$cellKey = array(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN'
);
//----------------------------------主表------------------------------------//
//处理表头标题
// $objPHPExcel->getActiveSheet()->mergeCells('A1:' . $cellKey[count($header) - 1] . '1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错)
// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title);
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//处理表头
foreach ($header as $k => $v) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k] . $topNumber, $v);//设置表头数据
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k] . $topNumber)->getFont()->setBold(true);//设置是否加粗
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k] . $topNumber)->getFill()->applyFromArray([
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'F2F2F2'
)
]);//设置是否加粗
$objPHPExcel->getActiveSheet()->getStyle($cellKey[$k] . $topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
$objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth(15);//设置列宽度
}
$objPHPExcel->getActiveSheet()
->getStyle('A1:AM1')
->getFill()
->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('FFFF00');
//----------------------------------主表------------------------------------//
//处理映射关系
$kv = [
"A" => "staffId",
"B" => "staffName",
"C" => "mobile",
"D" => "dept",
"E" => "email",
"F" => "positionName",
"G" => "postName",
"H" => "staffCode",
"I" => "roster",
"J" => "staffType",
"K" => "joinDate",
"L" => "probation",
"M" => "turnDate",
"N" => "idCard",
"O" => "birthday",
"P" => "gender",
"Q" => "nation",
"R" => "placeOfOrigin",
"S" => "householdRegisterType",
"T" => "householdLocation",
"U" => "curResidence",
"V" => "maritalStatus",
"W" => "politicsFace",
"X" => "socialAccount",
"Y" => "providentFundAccount",
"Z" => "education",
"AA" => "graduateSchool",
"AB" => "graduateDate",
"AC" => "major",
"AD" => "bankCardNum",
"AE" => "bankName",
"AF" => "contractName",
"AG" => "contractType",
"AH" => "contractStartAt",
"AI" => "contractEndAt",
"AJ" => "renewalNum",
"AK" => "contact",
"AL" => "relation",
"AM" => "contactMobile",
];
//-------简单的固定值--------//
$objActSheet = $objPHPExcel->getActiveSheet(0);
// 新建工作表 -->保存下拉框数据来源
$objPHPExcel->createSheet();
$subObject = $objPHPExcel->getSheet(0);
//设置下拉框
foreach ($spectials as $k => $spectial) {
//保存下拉框内容到新建的工作表中
$list = $spectial['select_options'];
if ($list) {
foreach ($list as $i => $d) {
$subcolumn = array_keys($kv);
$c = $subcolumn[$k] . ($i + 1);
if ($spectial['column'] == $c) {
$objActSheet->setCellValue($c, $d);
}
}
}
//下拉框写入开始行数
$n = 2;
// 我这里设置100行,可自行设置
while ($n < 800) {
$objValidation = $objActSheet->getCell($spectial['column'] . (string)$n)->getDataValidation(); //这一句为要设置数据有效性的单元格
// $objValidation的各项设置参数可详见phpexcel文件,
// 目录大概为/.../phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel5/Worksheet.php
// 2767行 方法名:writeDataValidity
$objValidation->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_STOP)
->setAllowBlank(true)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内.')
->setPromptTitle('')
->setPrompt('')
->setOperator(\PHPExcel_Cell_DataValidation::OPERATOR_BETWEEN)
->setFormula1("data!$" . $subcolumn[$k] . "$1:$" . $subcolumn[$k] . "$" . count($list)); //下拉框数据来源多的时候,建议使用此方法
// -> setFormula1('"'.$optionsString.'"'); //如果下拉框数据来源少,可使用此方法,无需新建表格
$n++;
}
}
$data = [$data[0]];
//----------------------------------数据源-----------------------------------//
$subObject = $objPHPExcel->getActiveSheet();
foreach ($data as $key => $value) {
foreach ($header as $k => $v) {
if (isset($kv[$cellKey[$k]])) {
$subObject->setCellValueExplicit($cellKey[$k] . ($key + 1 + $topNumber), $value[$kv[$cellKey[$k]]] ?? "");
} else {
break;
}
}
}
//----------------------------------数据源-----------------------------------//
//---------------------------------设置下拉框-----------------------------------//
//-----------复杂的-------------//
//导出execl
ob_end_clean();//清除缓冲区,避免乱码
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Content-Type:application/force-download');
header("Content-Type:application/vnd.ms-excel");
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header("Content-Disposition:" . $this->downloadHeaders("$fileName.xlsx"));
header('Content-Transfer-Encoding:binary');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
public function downloadHeaders($filename, $charset = 'UTF-8')
{
// 兼容各浏览器下载文件名乱码问题
if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
$filename = urlencode($filename);
$filename = str_replace("+", "%20", $filename);// 替换空格
$attachment = "attachment; filename=\"{$filename}\"; charset={$charset}";
} else if (preg_match("/Firefox/", $_SERVER["HTTP_USER_AGENT"])) {
$attachment = 'attachment; filename*=utf-8\'\'' . $filename;
} else if (preg_match("/(Safari)|(iphone)|(ipad)/i", $_SERVER["HTTP_USER_AGENT"])) {
$filename = rawurlencode($filename); // 注意:rawurlencode与urlencode的区别
$attachment = 'attachment; filename*=utf-8\'\'' . $filename;
} else {
$attachment = "attachment; filename=\"{$filename}\"; charset={$charset}";
}
return $attachment;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790