Advertisement
eyuprog

Import Excel ke MySQL Codeigniter

Oct 6th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.68 KB | None | 0 0
  1. Download http://phpexcel.codeplex.com/ masukin ke folder libraries
  2. ada 1 file PHPExcel.php dan 1 Folder PHPExcel
  3. *Bootstrap CSS (tambahkan bootstrap link)
  4. *buat tabel yg sama dengan nama sheet, jika menggunakan prefix, maka input table dibuat tanpa prefix
  5.  
  6. //////////////
  7. Pada View uploadexcel.php
  8. /////////////
  9. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.2.0/united/bootstrap.min.css">
  10. <?php
  11. $att=array(
  12.     'role'=>'form',
  13.     'class'=>'form-horizontal',
  14.     );
  15. echo  form_open_multipart(base_url('export/exportapply'),$att);
  16. ?>
  17.                                     <div class="box-body">
  18.                                    
  19.                                         <div class="form-group">
  20.                                             <label for="l" class="col-sm-2 control-label">File</label>
  21.                                             <div class="col-sm-7">
  22.                                             <input type="file" name="file" id="file" class="form-control" />
  23.                                             </div>
  24.                                         </div>
  25.                                                                            
  26.                                                                            
  27.                                     </div>
  28.                                     <div class="form-group">
  29.                                             <label for="l" class="col-sm-2 control-label">Nama Tabel</label>
  30.                                             <div class="col-sm-5">
  31.                                             <input type="text" name="tabel" class="form-control" value="" placeholder="Nama Tabel" required="required">
  32.                                             </div>
  33.                                         </div>
  34.                                        
  35.                                     <div class="form-group">
  36.                                             <label for="l" class="col-sm-2 control-label">&nbsp;</label>
  37.                                             <div class="col-sm-4">
  38.                                             <button type="submit" class="btn btn-primary"><li class="fa fa-save"> Import</li></button>
  39.                                            
  40.                                             </div>
  41.                                     </div>
  42.                                    
  43.                                    
  44.                                    
  45. </form>
  46.  
  47.  
  48. // Controller //
  49. ///////////////////////////
  50.  
  51. //export.php
  52.  
  53. <?php
  54. require_once APPPATH."/libraries/PHPExcel".EXT;
  55. class Export extends CI_Controller
  56. {
  57.        
  58.     function index()
  59.     {
  60.        
  61.         $this->load->view('uploadexcel');
  62.        
  63.     }
  64.    
  65.     function exportapply()
  66.     {
  67.         $file   = explode('.',$_FILES['file']['name']);
  68.         $length = count($file);
  69.         if($file[$length -1] == 'xlsx' || $file[$length -1] == 'xls'){
  70.             $tmp    = $_FILES['file']['tmp_name'];
  71.             $this->load->library('phpexcel');
  72.             $read   = PHPExcel_IOFactory::createReaderForFile($tmp);
  73.             $read->setReadDataOnly(true);
  74.             $excel  = $read->load($tmp);
  75.             $sheets = $read->listWorksheetNames($tmp);
  76.             $tabel=$this->input->post('tabel');
  77.             foreach($sheets as $sheet){
  78.                
  79.               //  if($this->db->table_exists($sheet)){
  80.                     $_sheet = $excel->setActiveSheetIndexByName($sheet);
  81.                     $maxRow = $_sheet->getHighestRow();
  82.                     $maxCol = $_sheet->getHighestColumn();
  83.                     $field  = array();
  84.                     $sql    = array();
  85.                     $maxCol = range('A',$maxCol);
  86.                     foreach($maxCol as $key => $coloumn){
  87.                         $field[$key]    = $_sheet->getCell($coloumn.'1')->getCalculatedValue();
  88.                     }
  89.                     for($i = 2; $i <= $maxRow; $i++){
  90.                         foreach($maxCol as $k => $coloumn){
  91.                             $sql[$field[$k]]  = $_sheet->getCell($coloumn.$i)->getCalculatedValue();
  92.                         }
  93.                         $this->db->insert($tabel,$sql);
  94.                     }
  95.               //  }else{
  96.             //      exit('Table tidak valid');
  97.             //  }
  98.             }
  99.             exit('Import Sukses')
  100.         }else{
  101.             exit('do not allowed to upload');
  102.         }
  103.        
  104.       }
  105. }
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement