ExportData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Category;
  4. use app\common\controller\Api;
  5. use think\Db;
  6. /**
  7. * 数据导出
  8. * Class ExportData
  9. * @package app\api\controller
  10. */
  11. class ExportData extends Api{
  12. protected $noNeedLogin = '*';
  13. protected $noNeedRight = '*';
  14. /**
  15. * 导出
  16. * @throws \PHPExcel_Reader_Exception
  17. * @throws \PHPExcel_Writer_Exception
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function export(){
  23. $id = $this->request->post("id",0);
  24. vendor('PHPExcel.PHPExcel');
  25. $fileName = '保险'."_" . date("Y_m_d_H_i_s",time()) . ".xls";
  26. //$file = '456.xls';
  27. $file_path = 'download/'.$fileName;
  28. $PHPExcel = new \PHPExcel();
  29. $PHPSheet = $PHPExcel->getActiveSheet();
  30. $PHPSheet->setTitle("保险"); //给当前活动sheet设置名称
  31. $result = Db::name('product')->where(array('category_id'=>$id))
  32. ->field('id,name,content,sort')->order('sort asc')->select();
  33. $zm = ['C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  34. $id_names = Db::name('category')->where(array('id'=>$id))->value('name');
  35. $PHPSheet->setCellValue("A1",'排名');
  36. $PHPSheet->setCellValue("B1",$id_names);
  37. $i = 2;
  38. $j = 1;
  39. // if($result){
  40. // $category_content = Db::name('Category')->where(array('id'=>$id))->value('content');
  41. // $content = json_decode($category_content,true);
  42. // p($content);
  43. // die;
  44. // }
  45. if($result){
  46. // foreach ($result as $k=>$v){
  47. // $result[$k]['len'] = mb_strlen($result[$k]['content'],'UTF8');
  48. // }
  49. // $len = array_column($result,'len');
  50. // array_multisort($len,SORT_DESC,$result);
  51. foreach ($result as $key=>$value){
  52. $outh = json_decode($result[$key]['content'],true);
  53. $category_content = Db::name('Category')->where(array('id'=>$id))->value('content');
  54. $content = json_decode($category_content,true);
  55. $c = $content;
  56. $count = count($content);
  57. //$end = '';
  58. foreach ($c as $k=>$v){
  59. if($key == 0){
  60. $PHPSheet->setCellValue($zm[$k].'1',$v['key']);
  61. $end = $zm[$k].'1';
  62. }
  63. $PHPSheet->setCellValue($zm[$count].'1','其他属性');
  64. $PHPSheet->getColumnDimension($zm[$count])->setWidth(40);
  65. $PHPSheet->setCellValue('A'.$i,$value['sort']);
  66. $PHPSheet->setCellValue('B'.$i,$value['name']);
  67. if(isset($v['name'])){
  68. $PHPSheet->setCellValue($zm[$k].$i,$outh[$k]['name']);
  69. }else{
  70. $PHPSheet->setCellValue($zm[$k].$i,'无');
  71. }
  72. $PHPSheet->getColumnDimension($zm[$k])->setWidth(12);
  73. $PHPSheet->getRowDimension($j)->setRowHeight(20);
  74. // if($k >= 1){
  75. //
  76. // }
  77. // $PHPSheet->setCellValue($zm[$count].$k,'其他属性11111');
  78. }
  79. if($outh){
  80. $con = '';
  81. $outh = $this->test($outh,$content);
  82. foreach ($outh as $k=>$v){
  83. $con .= $outh[$k]['key'].' : '.$outh[$k]['name']." \r\n ";
  84. }
  85. }
  86. $PHPSheet->getStyle()->getAlignment($zm[$count].($key+2))->setWrapText(true);
  87. $PHPSheet->setCellValue($zm[$count].($key+2),$con);
  88. $end = $zm[$count].'1';
  89. $j++;
  90. $i++;
  91. }
  92. $PHPSheet->getStyle('A1:'.$end)->getFont()->setBold(true)->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
  93. $PHPSheet->getStyle('A1:'.$end)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('094EC0');
  94. //合并单元格
  95. // $PHPSheet->mergeCells('A1:B1');
  96. //设置行高
  97. $PHPSheet->getRowDimension('1')->setRowHeight(30); //第一行行高
  98. $PHPSheet->getColumnDimension('B')->setWidth(20);
  99. // $PHPSheet->getColumnDimension('i')->setWidth(40);
  100. $PHPSheet->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  101. //$PHPSheet->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_LEFT);
  102. //$PHPSheet->setCellValue("A1",$id_names);
  103. // $PHPSheet->getStyle("A1:".$end)->getFont()->setBold(true);
  104. //$PHPSheet->getStyle("A1:".$end)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  105. //$PHPSheet->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  106. // $PHPSheet->getStyle("A1:".$end)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //左右居中
  107. //
  108. // $PHPSheet->getStyle("A1:".$end)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  109. $list['file_path'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$file_path;
  110. $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
  111. $objWriter->save($file_path); //文件直接下载我指定的服务器目录中
  112. $this->success(__('成功'),$list);
  113. }else{
  114. $this->error('没有数据');
  115. }
  116. }
  117. public function test($row = [],$rss = []){
  118. $content = $row; //当前全部属性
  119. $content_one = $rss; //模板属性
  120. $cha = [];//客户顾定模板设置
  121. $cha_arr = []; //自由追加的数据
  122. if($content)
  123. {
  124. foreach ($content as $key=>$val)
  125. {
  126. if(!isset($val['key'])) continue;
  127. if(isset($content_one[$key]))
  128. { //print_r($content_one[$key]);
  129. /// var_dump($val['key']);
  130. if ($val['key'] == $content_one[$key]['key'])
  131. {
  132. $cha[] = $val;
  133. }
  134. else
  135. {
  136. $cha_arr[] = $val;
  137. }
  138. } else {
  139. $cha_arr[] = $val;
  140. }
  141. }
  142. return $cha_arr;
  143. }
  144. }
  145. /**
  146. * 获取一级分类
  147. */
  148. public function getPrimaryClassification(){
  149. $list = Category::where(array('pid'=>0))->field('id,name,type')->select();
  150. $this->success(__('成功'),$list);
  151. }
  152. /**
  153. * 获取二级分类
  154. */
  155. public function getSecondaryClassification(){
  156. $id = $this->request->post("id",0);
  157. $list = Category::where(array('pid'=>$id))->field('id,name,type')->select();
  158. $this->success(__('成功'),$list);
  159. }
  160. /**
  161. * 获取三级分类
  162. */
  163. public function getThreeClassification(){
  164. $id = $this->request->post("id",0);
  165. $list = Category::where(array('pid'=>$id))->field('id,name,type')->select();
  166. $this->success(__('成功'),$list);
  167. }
  168. /**
  169. * 导出所有用户数据
  170. * 直接url访问,不能使用ajax,因为ajax要求返回数据,和PHPExcel一会浏览器输出冲突!将数据作为参数
  171. * @throws \PHPExcel_Exception
  172. * @throws \PHPExcel_Writer_Exception
  173. * @throws \think\Exception
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. * @throws \think\exception\DbException
  177. */
  178. public function out(){
  179. $result = Db::name('category')->field('id,name,pid')->order('id desc')->select();
  180. $filename = "用户测评数据";
  181. vendor('PHPExcel.PHPExcel');
  182. $objPHPExcel = new \PHPExcel();
  183. //设置保存版本格式
  184. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  185. //设置表头
  186. $objPHPExcel->getActiveSheet()->setCellValue('A1','id');
  187. $objPHPExcel->getActiveSheet()->setCellValue('B1','姓名');
  188. $objPHPExcel->getActiveSheet()->setCellValue('C1','手机');
  189. $objPHPExcel->getActiveSheet()->setCellValue('D1','意向院校');
  190. $objPHPExcel->getActiveSheet()->setCellValue('E1','测评分值');
  191. $objPHPExcel->getActiveSheet()->setCellValue('F1','学历背景得分');
  192. $objPHPExcel->getActiveSheet()->setCellValue('G1','企业背景得分');
  193. $objPHPExcel->getActiveSheet()->setCellValue('H1','收入水平得分');
  194. $objPHPExcel->getActiveSheet()->setCellValue('I1','发展潜力得分');
  195. $objPHPExcel->getActiveSheet()->setCellValue('J1','管理经验得分');
  196. $objPHPExcel->getActiveSheet()->setCellValue('K1','英语水平得分');
  197. $objPHPExcel->getActiveSheet()->setCellValue('L1','背景优势');
  198. $objPHPExcel->getActiveSheet()->setCellValue('M1','背景劣势');
  199. $objPHPExcel->getActiveSheet()->setCellValue('N1','匹配院校');
  200. //改变此处设置的长度数值
  201. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
  202. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
  203. //输出表格
  204. $str = '用户未进行测评';
  205. foreach ($result as $key => &$val) {
  206. $i=$key+2;//表格是从2开始的
  207. $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$val['id']);
  208. $objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$val['name']);
  209. $objPHPExcel->getActiveSheet()->setCellValue('C'.$i,$val['pid']);
  210. }
  211. $fileName = iconv("utf-8", "gb2312", $filename); // 重命名表
  212. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  213. header('Content-Type: application/vnd.ms-excel');
  214. header("Content-Disposition: attachment;filename='$fileName'");
  215. header('Cache-Control: max-age=0');
  216. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  217. $objWriter->save('php://output'); // 文件通过浏览器下载
  218. exit();
  219. //
  220. // header("Pragma: public");
  221. // header("Expires: 0");
  222. // header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  223. // header("Content-Type:application/force-download");
  224. // header("Content-Type:application/vnd.ms-execl");
  225. // header("Content-Type:application/octet-stream");
  226. // header("Content-Type:application/download");
  227. // header('Content-Disposition:attachment;filename='.$filename.'.xls');
  228. // header("Content-Transfer-Encoding:binary");
  229. // $objWriter->save('php://output');
  230. }
  231. }