请选择 进入手机版 | 继续访问电脑版

php后台execl数据日期筛选导出功能程序源码

[复制链接]
查看1502 | 回复0 | 2021-10-24 19:59 | 显示全部楼层 |阅读模式

php后台execl日期筛选导出功能程序源码

php后台execl数据日期筛选导出功能程序源码

php后台execl数据日期筛选导出功能程序源码

  1. public function export_excel(){
  2.             
  3.         set_time_limit(3600);
  4.         vendor('Excel.PHPExcel');
  5.         //$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;  
  6.         //PHPExcel_Settings::setCacheStorageMethod($cacheMethod);  
  7.         $cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;  
  8.         $cacheSettings = array('memoryCacheSize' => '16MB');  
  9.         PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
  10.         $objPHPExcel = new PHPExcel();
  11.         $file_name = 'product'.date('Ymd');
  12.         
  13.         $g_name = I('g_name', '', 'htmlspecialchars,trim');
  14.         $g_id = I('g_id', 0, 'intval');
  15.         $g_common = I('g_common', '', 'htmlspecialchars,trim');
  16.         $g_sn = I('g_sn', '', 'htmlspecialchars,trim');
  17.         $g_cate = I('g_cate', 0, 'intval');
  18.         $g_brand = I('g_brand', '', 'htmlspecialchars,trim');
  19.         $g_brand_id = I('g_brand_id', 0, 'intval');
  20.         $g_is_drug = I('is_drug', 0, 'intval');
  21.         $g_spec = I('g_spec', '', 'htmlspecialchars,trim');
  22.         $g_spec_id = I('g_spec_id', 0, 'intval');
  23.         $g_manufacturer = I('g_manufacturer', '', 'htmlspecialchars,trim');
  24.         $g_manufacturer_id = I('g_manufacturer_id', 0, 'intval');
  25.         $g_license = I('g_license', 0, 'htmlspecialchars,trim');
  26.         $g_license_id = I('g_license_id', 0, 'intval');
  27.         $g_is_on_sale = I('g_is_on_sale', 0, 'intval');
  28.         $g_department = I('g_department', 0, 'intval');
  29.         $g_stock = I('g_stock', 0, 'intval');
  30.         $g_is_code = I('g_is_code', 0, 'intval');
  31.         $g_code = I('g_code', 0, 'htmlspecialchars,trim');
  32.         $g_purchase_cate = I('g_purchase_cate', 0, 'intval');
  33.         $add_time1 = I('add_time1', '', 'htmlspecialchars,trim');
  34.         $add_time2 = I('add_time2', '', 'htmlspecialchars,trim');
  35.         
  36.         $field = array('g.new_goods_sn, g.goods_name, g.keywords, g.effect, g.common_name, g.license_number, gs.spec_name, g.is_drug, g.drug_description, g.form, g.goods_name as new_goods_name, g.material, g.leading_effect, g.crowd, gs.spec_name as new_spec_name, g.instructions, m.manufacturer_name, b.brand_name, gt.type_name, g.is_import, c.cate_name, g.haipin_id, g.is_on_sale, g.stock, g.code');
  37.         $table = 'pro_goods g';
  38.         $where = array('g.status'=>1, 'g.cate_id'=>array('neq', 9));
  39.         if($g_id){
  40.             $kwhere['g.goods_id']  = array('eq', $g_id);
  41.         }else{
  42.             if($g_name){
  43.                 $kwhere['g.goods_name']  = array('like',array('%'.$g_name.'%'));
  44.             }
  45.         }
  46.         if($g_common){
  47.             $kwhere['g.common_name']  = array('like',array('%'.$g_common.'%'));
  48.         }
  49.         if($g_sn){
  50.             $kwhere['g.new_goods_sn']  = array('like',array('%'.$g_sn.'%'));
  51.         }
  52.         if($g_cate){
  53.             $kwhere['g.cate_id']  = array('eq', $g_cate);
  54.         }
  55.         if($g_brand_id){
  56.             $kwhere['g.brand_id']  = array('eq', $g_brand_id);
  57.         }else{
  58.             if($g_brand){
  59.                 $kwhere['g.brand_id'] = array('in', $this->getList('Brand', 'brand_name', $g_brand, 'b_id'));
  60.             }
  61.         }
  62.         if($g_is_drug){
  63.             $kwhere['g.is_drug']  = array('eq', $g_is_drug);
  64.         }
  65.         if($g_spec_id){
  66.             $kwhere['g.spec_id']  = array('eq', $g_spec_id);
  67.         }else{
  68.             if($g_spec){
  69.                 $kwhere['g.spec_id'] = array('in', $this->getList('Goods_spec', 'spec_name', $g_spec, 's_id'));
  70.             }
  71.         }        
  72.         if($g_manufacturer_id){
  73.             $kwhere['g.manufacturer_id']  = array('eq', $g_manufacturer_id);
  74.         }else{
  75.             if($g_manufacturer){
  76.                 $kwhere['g.manufacturer_id'] = array('in', $this->getList('Manufacturer', 'manufacturer_name', $g_manufacturer, 'm_id'));
  77.             }
  78.         }   
  79.         if($g_license_id){
  80.             $kwhere['g.goods_id']  = array('eq', $g_license_id);
  81.         }else{
  82.             if($g_license){
  83.                 $kwhere['g.license_number']  = array('like',array('%'.$g_license.'%'));
  84.             }   
  85.         }
  86.         if($g_is_on_sale){
  87.             $kwhere['g.is_on_sale']  = array('eq', $g_is_on_sale-1);
  88.         }
  89.         if($g_department){
  90.             $kwhere['g.department_id']  = array('eq', $g_department);
  91.         }
  92.         if($g_stock){
  93.             $g_stock = $g_stock-1;
  94.             if(empty($g_stock)){
  95.                 $kwhere['g.stock']  = array('eq', $g_stock);
  96.             }else{
  97.                 $kwhere['g.stock']  = array('gt', 0);
  98.             }
  99.         }
  100.         if($g_is_code){
  101.             $g_is_code = $g_is_code-1;
  102.             if(empty($g_is_code)){
  103.                 $kwhere['g.code']  = array('eq', '');
  104.             }else{
  105.                 $kwhere['g.code']  = array('neq', '');
  106.             }
  107.         }
  108.         if($g_code){
  109.             $kwhere['g.code'] = array('like', array('%'.$g_code.'%'));
  110.         }
  111.         if($g_purchase_cate){
  112.             $kwhere['g.purchase_cate']  = array('eq', $g_purchase_cate);
  113.         }
  114.         //日期索引
  115.         if($add_time1){
  116.             if($add_time2){
  117.                 $add_time1 = strtotime($add_time1.' 00:00:00');
  118.                 $add_time2 = strtotime($add_time2.' 23:59:59');
  119.                 if($add_time2 < $add_time1){
  120.                     $this->error('申请时间到期时间不能小于开始时间!');
  121.                 }else{
  122.                     $kwhere['g.add_time'] = array('between', array($add_time1, $add_time2));
  123.                 }
  124.             }else{
  125.                 $add_time1 = strtotime($add_time1.' 00:00:00');
  126.                 $kwhere['g.add_time'] = array('egt', $add_time1);
  127.             }
  128.         }else{
  129.             if($add_time2){
  130.                 $add_time2 = strtotime($add_time2.' 23:59:59');
  131.                 $kwhere['g.add_time'] = array('elt', $add_time2);
  132.             }
  133.         }
  134.         
  135.         if(count($kwhere)){
  136.             $kwhere['_logic'] = 'AND';
  137.             $where['_complex'] = $kwhere;
  138.         }
  139.         
  140.         $count = M('Goods')->table($table)
  141.                 ->join('LEFT JOIN pro_brand b ON g.brand_id=b.b_id')
  142.                 ->join('LEFT JOIN pro_goods_spec gs ON g.spec_id=gs.s_id')
  143.                 ->join('LEFT JOIN pro_cate c ON g.cate_id=c.cate_id')
  144.                 ->join('LEFT JOIN pro_manufacturer m ON g.manufacturer_id=m.m_id')
  145.                 ->join('LEFT JOIN pro_goods_type gt ON g.type_id=gt.t_id')
  146.                 ->where($where)->count();
  147.                
  148.         $page = 1;
  149.         $page_number = 1024;
  150.         $count += $page_number;
  151.         $arr = array();
  152.         $is_drug = C(IS_DRUG);
  153.         $begin = ($page -1) * $page_number;
  154.         while($begin < $count){            
  155.             $res = M('Goods')->table($table)
  156.                     ->join('LEFT JOIN pro_brand b ON g.brand_id=b.b_id')
  157.                     ->join('LEFT JOIN pro_goods_spec gs ON g.spec_id=gs.s_id')
  158.                     ->join('LEFT JOIN pro_cate c ON g.cate_id=c.cate_id')
  159.                     ->join('LEFT JOIN pro_manufacturer m ON g.manufacturer_id=m.m_id')
  160.                     ->join('LEFT JOIN pro_goods_type gt ON g.type_id=gt.t_id')
  161.                     ->where($where)->field($field)->limit($begin, $page_number)->select();
  162.                     
  163.             foreach($res as $key=>$val){
  164.                 $res[$key]['is_drug'] = $is_drug[$val['is_drug']];
  165.                 $res[$key]['is_import'] = $val['is_import']? '是': '';
  166.                 $res[$key]['is_on_sale'] = empty($val['is_on_sale'])? '否': '是';
  167.                 $new_key = $begin + $key;
  168.                 $arr[] = $res[$key];
  169.             }
  170.             unset($res);   
  171.             $page ++;
  172.             $begin = ($page -1) * $page_number;
  173.         }        
  174.         
  175.         $letter = 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');
  176.         $title = array("新编码", "商品名称", "商品关键字", "商品功效", "通用名称", "批准文号", "商品规格", "是否处方药", "处方药说明", "剂型", "产品名称", "主要原料", "主要作用", "适宜人群", "产品规格", "用法用量", "生产企业", "品牌名称", "药品类别", "是否进口", "商品分类", "海品id", "是否采购", "库存", "条形码");
  177.         
  178.         $objPHPExcel->setActiveSheetIndex(0);
  179.         $j =0;
  180.         foreach($title as $t_val){
  181.             $index = $letter[$j];
  182.             $objPHPExcel->getActiveSheet()->setCellValue($index."1", $t_val);
  183.             $j++;
  184.         }
  185.         
  186.         $arr_count = count($arr);
  187.         $i = 2;
  188.                         
  189.         foreach($arr as $key=>$val){            
  190.             if($key < $arr_count){
  191.                 $j =0;
  192.                 foreach($val as $val2){
  193.                     $index = $letter[$j];
  194.                     $objPHPExcel->getActiveSheet()->setCellValue($index.$i, $val2);
  195.                     $j++;
  196.                 }
  197.                 $i++;
  198.             }
  199.         }
  200.         
  201.         $objPHPExcel->getActiveSheet()->setTitle('商品信息表');
  202.         
  203.         header('Content-Type:application/vnd.ms-excel');
  204.         header('Content-Disposition:attachment;filename="'.$file_name.'.xls"');
  205.         header('Cache-Control:max-age=0');
  206.         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  207.         $objWriter->save('php://output');
  208.         exit;
  209.         
  210.      }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-3-24