Sfoglia il codice sorgente

添加导出excel功能

lgy 6 anni fa
parent
commit
d4f161d815

+ 140 - 0
application/manage/controller/Counts.php

@@ -98,4 +98,144 @@ class Counts extends Base {
         echo json_encode($data);
 
     }
+
+    public function out() {
+        $admin = Session::get('session_manager');
+        $institution_id = $admin['institution_id'];
+        $request = Request::instance();
+        $params = $request->param();
+        $where = array();
+        foreach ($params as $k=>$v) {
+            switch($k){
+                case 'upload1':
+                    if($params['upload1'] == null){
+                        continue;
+                    }
+                    $where[] = ' s.createdAt > \''.$params['upload1'].'\' ';
+                    break;
+                case 'upload2':
+                    if($params['upload2'] == null){
+                        continue;
+                    }
+                    $where[] = ' s.createdAt < \''.$params['upload2'].'\' ';
+                    break;
+                case 'exam1':
+                    if($params['exam1'] == null){
+                        continue;
+                    }
+                    $exam1 = str_replace('-', '', $params['exam1']);
+                    $where[] = ' s.studydate > \''.$exam1.'\' ';
+                    break;
+                case 'exam2':
+                    if($params['exam2'] == null){
+                        continue;
+                    }
+                    $exam2 = str_replace('-', '', $params['exam2']);
+                    $where[] = ' s.studydate < \''.$exam2.'\' ';
+                    break;
+                case 'exam_class':
+                    if($params['exam_class'] == null){
+                        continue;
+                    }
+                    $where[] = 's.modality=\''.$params['exam_class'].'\' ';
+                    break;
+            }
+        }
+        if(count($where) <= 1){
+            $where1  =  implode('',$where);
+            $search = $where1;
+        }else{
+            $where1 = implode(' and ',$where);
+            $search = $where1;
+        }
+        $data = DB::table('studies')
+            ->alias('s')
+            ->join(['patient_infos'=>'p'],'p.id=s.patient_id')
+            ->join(['user_bind'=>'u'],'u.patient_id=s.patient_id','left')
+            ->where('s.institution_id='.$institution_id)
+            ->where($search)
+            ->page(1, 20)
+            ->field('p.name,p.age,p.sex,s.studydate,s.createdAt,s.modality,u.exam_id')
+            ->order('s.createdAt desc')
+            ->select();
+        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
+        /*import("Org.Util.PHPExcel");
+        import("Org.Util.PHPExcel.Writer.Excel5");
+        import("Org.Util.PHPExcel.IOFactory.php");*/
+        foreach($data as $k=>$v){
+            if($v['sex'] == 'F'){
+                $data[$k]['sex'] = '女';
+            }elseif($v['sex'] == 'M'){
+                $data[$k]['sex'] = '男';
+            }else{
+                $data[$k]['sex'] = '未知';
+            }
+            if($v['exam_id'] == null){
+                $data[$k]['exam_id'] = '未绑定';
+            }else{
+                $data[$k]['exam_id'] = '已绑定';
+            }
+        }
+        $filename = time()."数据统计表";
+        $headArr = array("患者姓名","患者年龄", "患者性别","检查时间", "上传时间", "患者类型",  "是否绑定微信");
+        $this->getExcel($filename, $headArr, $data);
+    }
+    public function getExcel($fileName, $headArr, $data) {
+        //对数据进行检验
+        if (empty($data) || !is_array($data)) {
+            die("data must be a array");
+        }
+        //检查文件名
+        if (empty($fileName)) {
+            exit;
+        }
+
+        $date = date("Y_m_d", time());
+        $fileName .= "_{$date}.xls";
+
+        //创建PHPExcel对象,注意,不能少了\
+        $objPHPExcel = new \PHPExcel();
+        $objProps = $objPHPExcel->getProperties();
+
+        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
+        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
+
+        //设置表头
+        $key = ord("A");
+        foreach ($headArr as $v) {
+            $colum = chr($key);
+            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
+            $key += 1;
+        }
+
+        $column = 2;
+        $objActSheet = $objPHPExcel->getActiveSheet();
+        foreach ($data as $key => $rows) { //行写入
+            $span = ord("A");
+            foreach ($rows as $keyName => $value) {// 列写入
+                $j = chr($span);
+                $objActSheet->setCellValue($j . $column, $value);
+                $span++;
+            }
+            $column++;
+        }
+
+        $fileName = iconv("utf-8", "gb2312", $fileName);
+        //重命名表
+        // $objPHPExcel->getActiveSheet()->setTitle('test');
+        //设置活动单指数到第一个表,所以Excel打开这是第一个表
+        $objPHPExcel->setActiveSheetIndex(0);
+        header('Content-Type: application/vnd.ms-excel');
+        header("Content-Disposition: attachment;filename=\"$fileName\"");
+        header('Cache-Control: max-age=0');
+
+        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
+        $objWriter->save('php://output'); //文件通过浏览器下载
+        exit;
+    }
 }

+ 19 - 6
application/manage/view/counts/index.html

@@ -129,8 +129,18 @@
                 var url="/manage/counts/datas?exam_class="+(exam==null?"":exam)+"&upload1="+(upload1==null?"":upload1)+"&upload2="+(upload2==null?"":upload2)+"&exam1="+(exam1==null?"":exam1)+"&exam2="+(exam2==null?"":exam2);
                 $('#dg').datagrid("reload",url);
             }
-            
-        
+
+            function derive(){
+                var form = $("#form");
+                var upload1 = $("#upload1").val();
+                var upload2 = $("#upload2").val();
+                var exam1 = $("#exam1").val();
+                var exam2 = $("#exam2").val();
+                var exam=$('#exam_class').val();
+                var url="/manage/counts/out?exam_class="+(exam==null?"":exam)+"&upload1="+(upload1==null?"":upload1)+"&upload2="+(upload2==null?"":upload2)+"&exam1="+(exam1==null?"":exam1)+"&exam2="+(exam2==null?"":exam2);
+                form.attr('action',url);
+                form.submit();
+            }
             
             $(function(){
                 $('#institution_id').combotree({
@@ -146,10 +156,13 @@
     <body>
         <div id="dg" class="easyui-datagrid" style="width:100%;height:auto;" ></div>
         <div id="tb" class="editform" style="display: none; padding-top: 1px;">&nbsp;
-            检查类型:<input type="text" name="exam_class" id="exam_class" style="width:150px;" />&nbsp;
-            检查时间:<input type="text" id="exam1"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />----<input type="text" id="exam2" name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />&nbsp;&nbsp;&nbsp;
-            上传时间:<input type="text" id="upload1"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />----<input type="text" id="upload2"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />&nbsp;&nbsp;&nbsp;
-            <a id="submit" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width: 80px" onclick="reload()" >查询</a>
+            <form id="form">
+                检查类型:<input type="text" name="exam_class" id="exam_class" style="width:150px;" />&nbsp;
+                检查时间:<input type="text" id="exam1"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />----<input type="text" id="exam2" name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />&nbsp;&nbsp;&nbsp;
+                上传时间:<input type="text" id="upload1"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />----<input type="text" id="upload2"  name="upload_datetime[]" class="easyui-datebox" style="width: 120px;" />&nbsp;&nbsp;&nbsp;
+                <a id="submit" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width: 80px" onclick="reload()" >查询</a>&nbsp;&nbsp;&nbsp;
+                <a id="excel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width: 140px" onclick="derive()" >导出excel</a>
+            </form>
         </div>
         <div id="edit-window" class="easyui-window" style="overflow: hidden" ><iframe src="about:blank" id="editFrame" style="width:100%;height:100%" frameborder="0" scrolling="yes" ></iframe></div>