利用PHPExcel导出Excel并设置Excel格式以及数据源

时间:2022-11-13 14:01:52
浏览:23969
发布日期:2013/07/24 分类:技术分享

代码有点长,读起来有点累。先来个截图

利用PHPExcel导出Excel并设置Excel格式以及数据源

导出的Excel太宽了,所以将后面的列宽重新调整了再截的图



功能包括:

1、设置单元格格式,包括单元格边框、单元格高度、单元格宽度

2、合并指定的单元格

3、设置Excel数据源,并将数据源保护起来(这个是为了实现单元格下拉选项功能)

4、设置字体样式

  1. public function export(){
  2. //此处全是一维数组
  3. $resumeState = C('RESUME_STATE');
  4. $processingStatus = C('PROCESSING_STATUS');
  5. $hunyin = C('HUNYIN');
  6. $sex = C('SEX');
  7. $AQE = C('AQE');
  8. //面试企业列表
  9. $C = M('Company');
  10. $company = $C->where('status=1')->field('id,title')->order($this->order)->select();
  11. //需要被出的数据
  12. $condition = $this->condition;
  13. $Model = $this->model;
  14. $Dao = M($Model);
  15. $list = $Dao->where($condition)->select();
  16. if (empty($list)){
  17. $this->error('没有可以导出的简历');
  18. }
  19. vendor('PHPExcel.PHPExcel');
  20. $fileName = $this->fileName;
  21. $fileName = empty($fileName)?'导出简历-'.date('Y-m-d',time()):$fileName;
  22. $PHPExcel = new PHPExcel();
  23. //设置基本信息
  24. $PHPExcel->getProperties()->setCreator("jecken")
  25. ->setLastModifiedBy("jecken")
  26. ->setTitle("上海**人力资源服务有限公司")
  27. ->setSubject("简历列表")
  28. ->setDescription("")
  29. ->setKeywords("简历列表")
  30. ->setCategory("");
  31. $PHPExcel->setActiveSheetIndex(0);
  32. $PHPExcel->getActiveSheet()->setTitle($fileName);
  33. //取得HR列表
  34. $hrlist = M('Admin')->field('id,nickname')->select();
  35. foreach ($hrlist as $key => $value){
  36. $hr[$value['id']] = $value['nickname'];
  37. }
  38. //存储Excel数据源到其他工作薄
  39. $PHPExcel->createSheet();
  40. $subObject = $PHPExcel->getSheet(1);
  41. $subObject->setTitle('data');
  42. foreach ($resumeState as $key => $value){
  43. $subObject->setCellValue('A'.$key,$value);
  44. }
  45. foreach ($processingStatus as $key => $value){
  46. $subObject->setCellValue('B'.$key,$value);
  47. }
  48. foreach ($company as $key => $value){
  49. $subObject->setCellValue('C'.($key+1),$value['title']);
  50. $companyList[$value['id']]=$value['title'];
  51. }
  52. $subObject->getColumnDimension('A')->setWidth(30);
  53. $subObject->getColumnDimension('B')->setWidth(30);
  54. $subObject->getColumnDimension('C')->setWidth(30);
  55. //保护数据源
  56. $subObject->getProtection()->setSheet(true);
  57. $subObject->protectCells('A1:C1000',time());
  58. //填入主标题
  59. $PHPExcel->getActiveSheet()->setCellValue('A1', '上海**人力资源服务有限公司');
  60. //填入副标题
  61. $PHPExcel->getActiveSheet()->setCellValue('A2', '简历列表(导出日期:'.date('Y-m-d',time()).')');
  62. //填入表头
  63. $PHPExcel->getActiveSheet()->setCellValue('A3', 'ID');
  64. $PHPExcel->getActiveSheet()->setCellValue('B3', '姓名');
  65. $PHPExcel->getActiveSheet()->setCellValue('C3', '性别');
  66. $PHPExcel->getActiveSheet()->setCellValue('D3', '年龄');
  67. $PHPExcel->getActiveSheet()->setCellValue('E3', '联系方式');
  68. $PHPExcel->getActiveSheet()->setCellValue('F3', '学历');
  69. $PHPExcel->getActiveSheet()->setCellValue('G3', '是否有AQE证书');
  70. $PHPExcel->getActiveSheet()->setCellValue('H3', '住址');
  71. $PHPExcel->getActiveSheet()->setCellValue('I3', '面试日期');
  72. $PHPExcel->getActiveSheet()->setCellValue('J3', '面试时间');
  73. $PHPExcel->getActiveSheet()->setCellValue('K3', '所属HR');
  74. $PHPExcel->getActiveSheet()->setCellValue('L3', '备注');
  75. $PHPExcel->getActiveSheet()->setCellValue('M3', '证件号码');
  76. $PHPExcel->getActiveSheet()->setCellValue('N3', 'QQ');
  77. $PHPExcel->getActiveSheet()->setCellValue('O3', '电子邮箱');
  78. $PHPExcel->getActiveSheet()->setCellValue('P3', '出生日期');
  79. $PHPExcel->getActiveSheet()->setCellValue('Q3', '婚姻状况');
  80. $PHPExcel->getActiveSheet()->setCellValue('R3', '户籍');
  81. $PHPExcel->getActiveSheet()->setCellValue('S3', '毕业时间');
  82. $PHPExcel->getActiveSheet()->setCellValue('T3', '毕业院校');
  83. $PHPExcel->getActiveSheet()->setCellValue('U3', '专业');
  84. $PHPExcel->getActiveSheet()->setCellValue('V3', '工作经历');
  85. $PHPExcel->getActiveSheet()->setCellValue('W3', '期望工作地点');
  86. $PHPExcel->getActiveSheet()->setCellValue('X3', '期望薪酬');
  87. $PHPExcel->getActiveSheet()->setCellValue('Y3', '期望职业');
  88. $PHPExcel->getActiveSheet()->setCellValue('Z3', '面试单位');
  89. $PHPExcel->getActiveSheet()->setCellValue('AA3', '面试岗位');
  90. $PHPExcel->getActiveSheet()->setCellValue('AB3', '目前状态');
  91. $PHPExcel->getActiveSheet()->setCellValue('AC3', '处理状态');
  92. $PHPExcel->getActiveSheet()->setCellValue('AD3', '自我评价');
  93. $PHPExcel->getActiveSheet()->setCellValue('AE3', '其他');
  94. //填入列表
  95. $k = 1;
  96. foreach ($list as $key => $value){
  97. $k++;
  98. $PHPExcel->getActiveSheet()->setCellValue('A'.($key+4), $value['id']);
  99. $PHPExcel->getActiveSheet()->setCellValue('B'.($key+4), $value['name']);
  100. //性别
  101. $PHPExcel->getActiveSheet()->getCell('C'.($key+4))->getDataValidation()
  102. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  103. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  104. -> setAllowBlank(false)
  105. -> setShowInputMessage(true)
  106. -> setShowErrorMessage(true)
  107. -> setShowDropDown(true)
  108. -> setErrorTitle('请选择性别')
  109. -> setError('您输入的值不在下拉框列表内.')
  110. -> setPromptTitle('性别')
  111. -> setFormula1('"'.join(',', $sex).'"');
  112. $PHPExcel->getActiveSheet()->setCellValue('C'.($key+4), $sex[$value['sex']]);
  113. //年龄
  114. $PHPExcel->getActiveSheet()->setCellValue('D'.($key+4), $value['age']);
  115. $PHPExcel->getActiveSheet()->setCellValue('E'.($key+4), $value['tel']);
  116. $PHPExcel->getActiveSheet()->setCellValue('F'.($key+4), $value['xueli']);
  117. //是否有AQE证书
  118. $PHPExcel->getActiveSheet()->getCell('G'.($key+4))->getDataValidation()
  119. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  120. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  121. -> setAllowBlank(false)
  122. -> setShowInputMessage(true)
  123. -> setShowErrorMessage(true)
  124. -> setShowDropDown(true)
  125. -> setErrorTitle('请选择是否有AQE证书')
  126. -> setError('您输入的值不在下拉框列表内.')
  127. -> setPromptTitle('是否有AQE证书')
  128. -> setFormula1('"'.join(',', $AQE).'"');
  129. $PHPExcel->getActiveSheet()->setCellValue('G'.($key+4), $AQE[$value['hasAQE']]);
  130. $PHPExcel->getActiveSheet()->setCellValue('H'.($key+4), $value['juzhudi']);
  131. $PHPExcel->getActiveSheet()->setCellValue('I'.($key+4), setDate($value['auditionTime'],'Y年m月d日'));//面试日期
  132. $PHPExcel->getActiveSheet()->setCellValue('J'.($key+4), setDate($value['auditionTime'],'H点i分'));//面试时间
  133. $PHPExcel->getActiveSheet()->setCellValue('K'.($key+4), $hr[$value['userid']]); //所属HR
  134. $PHPExcel->getActiveSheet()->setCellValue('L'.($key+4), $value['remark']);//备注
  135. $PHPExcel->getActiveSheet()->setCellValue('M'.($key+4), $value['cid']);//证件号码
  136. $PHPExcel->getActiveSheet()->setCellValue('N'.($key+4), $value['qq']);
  137. $PHPExcel->getActiveSheet()->setCellValue('O'.($key+4), $value['email']);
  138. $PHPExcel->getActiveSheet()->setCellValue('P'.($key+4), setDate($value['birthday']));
  139. //婚姻
  140. $PHPExcel->getActiveSheet()->getCell('Q'.($key+4))->getDataValidation()
  141. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  142. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  143. -> setAllowBlank(false)
  144. -> setShowInputMessage(true)
  145. -> setShowErrorMessage(true)
  146. -> setShowDropDown(true)
  147. -> setErrorTitle('请选择婚姻')
  148. -> setError('您输入的值不在下拉框列表内.')
  149. -> setPromptTitle('性别')
  150. -> setFormula1('"'.join(',', $hunyin).'"');
  151. $PHPExcel->getActiveSheet()->setCellValue('Q'.($key+4), $hunyin[$value['hunyin']]);
  152. $PHPExcel->getActiveSheet()->setCellValue('R'.($key+4), $value['huji']);
  153. $PHPExcel->getActiveSheet()->setCellValue('S'.($key+4), setDate($value['graduationTime']));
  154. $PHPExcel->getActiveSheet()->setCellValue('T'.($key+4), $value['graduationSchool']);
  155. $PHPExcel->getActiveSheet()->setCellValue('U'.($key+4), $value['specialty']);
  156. $PHPExcel->getActiveSheet()->setCellValue('V'.($key+4), $value['works']);
  157. $PHPExcel->getActiveSheet()->setCellValue('W'.($key+4), $value['expectAddress']);
  158. $PHPExcel->getActiveSheet()->setCellValue('X'.($key+4), $value['expectSalary']);
  159. $PHPExcel->getActiveSheet()->setCellValue('Y'.($key+4), $value['expectProfession']);
  160. //面试单位
  161. $PHPExcel->getActiveSheet()->getCell('Z'.($key+4))->getDataValidation()
  162. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  163. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  164. -> setAllowBlank(false)
  165. -> setShowInputMessage(true)
  166. -> setShowErrorMessage(true)
  167. -> setShowDropDown(true)
  168. -> setErrorTitle('输入的值有误')
  169. -> setError('您输入的值不在下拉框列表内.')
  170. -> setPromptTitle('面试单位')
  171. -> setFormula1('data!$C$1:$C$'.count($company));
  172. $PHPExcel->getActiveSheet()->setCellValue('Z'.($key+4), $companyList[$value['company']]);//面试单位
  173. $PHPExcel->getActiveSheet()->setCellValue('AA'.($key+4), $value['post']); //面试岗位
  174. //简历状态
  175. $PHPExcel->getActiveSheet()->getCell('AB'.($key+4))->getDataValidation()
  176. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  177. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  178. -> setAllowBlank(false)
  179. -> setShowInputMessage(true)
  180. -> setShowErrorMessage(true)
  181. -> setShowDropDown(true)
  182. -> setErrorTitle('输入的值有误')
  183. -> setError('您输入的值不在下拉框列表内.')
  184. -> setPromptTitle('简历状态')
  185. -> setFormula1('data!$A$1:$A$'.count($resumeState));
  186. $PHPExcel->getActiveSheet()->setCellValue('AB'.($key+4), $resumeState[$value['resumeState']]);
  187. //处理状态
  188. $PHPExcel->getActiveSheet()->getCell('AC'.($key+4))->getDataValidation()
  189. -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
  190. -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
  191. -> setAllowBlank(false)
  192. -> setShowInputMessage(true)
  193. -> setShowErrorMessage(true)
  194. -> setShowDropDown(true)
  195. -> setErrorTitle('输入的值有误')
  196. -> setError('您输入的值不在下拉框列表内.')
  197. -> setPromptTitle('处理状态')
  198. -> setFormula1('data!$B$1:$B$'.count($processingStatus));
  199. $PHPExcel->getActiveSheet()->setCellValue('AC'.($key+4), $processingStatus[$value['processingStatus']]);
  200. $PHPExcel->getActiveSheet()->setCellValue('AD'.($key+4), $value['selfIntroduction']);
  201. $PHPExcel->getActiveSheet()->setCellValue('AE'.($key+4), $value['other']);
  202. //设置每一行行高
  203. $PHPExcel->getActiveSheet()->getRowDimension($key+4)->setRowHeight(30);
  204. }
  205. //合并单元格
  206. $PHPExcel->getActiveSheet()->mergeCells('A1:AE1');
  207. $PHPExcel->getActiveSheet()->mergeCells('A2:AE2');
  208. //设置单元格宽度
  209. $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
  210. $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
  211. $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
  212. $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
  213. $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
  214. $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  215. $PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);
  216. $PHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
  217. $PHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
  218. $PHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
  219. $PHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
  220. $PHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(30);
  221. $PHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
  222. $PHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
  223. $PHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(25);
  224. $PHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);
  225. $PHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(10);
  226. $PHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(25);
  227. $PHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(15);
  228. $PHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(18);
  229. $PHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(15);
  230. $PHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(30);
  231. $PHPExcel->getActiveSheet()->getColumnDimension('W')->setWidth(15);
  232. $PHPExcel->getActiveSheet()->getColumnDimension('X')->setWidth(15);
  233. $PHPExcel->getActiveSheet()->getColumnDimension('Y')->setWidth(15);
  234. $PHPExcel->getActiveSheet()->getColumnDimension('Z')->setWidth(20);
  235. $PHPExcel->getActiveSheet()->getColumnDimension('AA')->setWidth(20);
  236. $PHPExcel->getActiveSheet()->getColumnDimension('AB')->setWidth(20);
  237. $PHPExcel->getActiveSheet()->getColumnDimension('AC')->setWidth(15);
  238. $PHPExcel->getActiveSheet()->getColumnDimension('AD')->setWidth(30);
  239. $PHPExcel->getActiveSheet()->getColumnDimension('AE')->setWidth(30);
  240. //设置表头行高
  241. $PHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
  242. $PHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(22);
  243. $PHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(20);
  244. //设置字体样式
  245. $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
  246. $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
  247. $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  248. $PHPExcel->getActiveSheet()->getStyle('A3:AE3')->getFont()->setBold(true);
  249. $PHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
  250. $PHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(16);
  251. $PHPExcel->getActiveSheet()->getStyle('A4:AE'.($k+2))->getFont()->setSize(10);
  252. //设置居中
  253. $PHPExcel->getActiveSheet()->getStyle('A1:AE'.($k+2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  254. //所有垂直居中
  255. $PHPExcel->getActiveSheet()->getStyle('A1:AE'.($k+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  256. //设置单元格边框
  257. $PHPExcel->getActiveSheet()->getStyle('A3:AE'.($k+2))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  258. //设置自动换行
  259. $PHPExcel->getActiveSheet()->getStyle('A3:AE'.($k+2))->getAlignment()->setWrapText(true);
  260. //保存为2003格式
  261. $objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
  262. header("Pragma: public");
  263. header("Expires: 0");
  264. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  265. header("Content-Type:application/force-download");
  266. header("Content-Type:application/vnd.ms-execl");
  267. header("Content-Type:application/octet-stream");
  268. header("Content-Type:application/download");
  269. //多浏览器下兼容中文标题
  270. $encoded_filename = urlencode($fileName);
  271. $ua = $_SERVER["HTTP_USER_AGENT"];
  272. if (preg_match("/MSIE/", $ua)) {
  273. header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
  274. } else if (preg_match("/Firefox/", $ua)) {
  275. header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '.xls"');
  276. } else {
  277. header('Content-Disposition: attachment; filename="' . $fileName . '.xls"');
  278. }
  279. header("Content-Transfer-Encoding:binary");
  280. $objWriter->save('php://output');
  281. }