wzh 4 anni fa
parent
commit
93ff1ea4d4
1 ha cambiato i file con 103 aggiunte e 0 eliminazioni
  1. 103 0
      api/application/helpers/function_helper.php

+ 103 - 0
api/application/helpers/function_helper.php

@@ -1008,4 +1008,107 @@ function alarm_translate_ch($msg){
     return isset($data[$msg]) ? $data[$msg] : '';
 }
 
+ /* 导出excel函数*/
+function push_excel($data,$fileName='Excel',$type = 0){
+    set_time_limit(120);
+    if (empty($type)) {
+        // header('Content-Type: application/vnd.ms-excel');
+        // header('Content-Disposition: attachment;filename="amazon_product_quantity.csv"');
+        // header('Cache-Control: max-age=0');
+        // $res = (new ListModel);
+        // $counts = $res->query('select count(*) from test');
+        $counts = count($data);
+        $coun = 1000;
+        $limit = ceil($counts/$coun);
+        // 打开PHP文件句柄,php://output 表示直接输出到浏览器
+        $filePath = FCPATH.'../upload/file/';
+        if (!file_exists(FCPATH.'../upload/file')) {
+            mkdir(FCPATH.'../upload/file');
+        }
+        $_savePath = $filePath.$fileName.'.csv'; 
+        $fp = fopen($_savePath, 'w');
+        $head = $data[0];
+        foreach ($head as $i => $v) {
+            // CSV的Excel支持GBK编码,一定要转换,否则乱码
+            $head[$i] =iconv("utf-8","gb2312//IGNORE",$v);
+        }
+        array_shift($data);
+        fputcsv($fp, $head);
+        $i = 1;
+        $n = 1;
+        while($n <= $limit){
+            $max = $counts >= $n*$coun ? $n*$coun : $counts-1;
+            for ($s=($n-1)*$coun; $s < $max; $s++) { 
+                $temp = array();
+                foreach ($data[$s] as $x => $y) {
+                    // CSV的Excel支持GBK编码,一定要转换,否则乱码
+                    $y = $y."\t";
+                    $temp[$x] =iconv("utf-8","gb2312//IGNORE",$y);
+                }
+                fputcsv($fp, $temp);
+                $i++;
+            }
+            if($i>20000){//读取一部分数据刷新下输出buffer
+                ob_flush();
+                flush();
+                $i = 0;
+            }
+            $n++;
+            /*
+            if($n == 300){
+            break;
+            }
+            */
+        }
+        return base_url('upload/file/'.$fileName.'.csv'); 
+    }else{
+        require_once FCPATH . 'application/third_party/phpExcel/PHPExcel.php';
+        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
+        if (!\PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
+            die($cacheMethod . " 缓存方法不可用" . EOL);
+        }
+        $obj = new PHPExcel();  
+      
+        //横向单元格标识  
+        $cellName = 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', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
+          
+        
+        //填写数据  
+        if($data){  
+            foreach($data AS $k=>$_v){  
+                if ($k > 0) {
+                    $obj->createSheet();
+                }
+                $obj->setactivesheetindex($k);
+                $obj->getActiveSheet()->setTitle($_v['name']);   //设置sheet名称
+                $i = 0;
+                foreach ($_v['dataList'] as $k2=>$d) {
+                    $_row = 1;   //设置纵向单元格标识
+                    $j = 0;  
+                    foreach($d AS $_cell){  
+                        $obj->getActiveSheet()->setCellValue($cellName[$j] . ($i+$_row), $_cell); 
+                        $j++;  
+                    }  
+                    $i++; 
+                }
+                 
+            }  
+        }  
+        
+        $filePath = FCPATH.'file/temp/';
+        if (!file_exists(FCPATH.'file/temp')) {
+            mkdir(FCPATH.'file/temp');
+        }
+      
+        $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel5'); 
+      
+        $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码  
+        $_savePath = $filePath.$_fileName.'.xls';  
+        
+        $objWrite->save($_savePath);  
+        
+        return base_url('api/file/temp/'.$fileName.'.xls'); 
+    }
+}
+
 ?>