ExcelToJson.php
[Pman.Core] / ExcelToJson.php
index cad6033..ffda332 100644 (file)
@@ -34,4 +34,58 @@ class Pman_Core_ExcelToJson extends Pman_Roo
         $this->jdata($data);
         
     }
+    
+    function importCsv($csv)
+    {
+        ini_set("auto_detect_line_endings", true);
+        
+        $fh = fopen($csv, 'r');
+        if (!$fh) {
+            $this->jerr("invalid file");
+        }
+        
+         
+        $cols = false;
+        $header = false;
+        $rows = array();
+        $ret = array();
+        
+        while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
+            if(!array_filter($n)){
+                if ($header) {
+                    continue;
+                }
+                $header = true;
+                continue;
+            }
+            
+            if(!$header){
+               $ret[preg_replace(array('/\s/', '/\:/'), '', $n[0])] = $n[1];
+               continue;
+            }
+            
+            if(!$cols){
+                $cols = array();
+                foreach($n as $k) {
+                    $cols[] = strtoupper(trim($k));
+                }
+                
+                if (empty($cols)) {
+                    continue;
+                }
+               
+           
+                continue;
+            }
+            
+            foreach($cols as $i=>$k) {
+                $row[$k] = $n[$i];
+            }
+            $rows[] = $row;
+            
+        }
+        fclose($fh);
+        
+        return array('ret' => $ret, 'rows' => $rows);;
+    }
 }
\ No newline at end of file