Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Download http://phpexcel.codeplex.com/ masukin ke folder libraries
- ada 1 file PHPExcel.php dan 1 Folder PHPExcel
- *Bootstrap CSS (tambahkan bootstrap link)
- *buat tabel yg sama dengan nama sheet, jika menggunakan prefix, maka input table dibuat tanpa prefix
- //////////////
- Pada View uploadexcel.php
- /////////////
- <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.2.0/united/bootstrap.min.css">
- <?php
- $att=array(
- 'role'=>'form',
- 'class'=>'form-horizontal',
- );
- echo form_open_multipart(base_url('export/exportapply'),$att);
- ?>
- <div class="box-body">
- <div class="form-group">
- <label for="l" class="col-sm-2 control-label">File</label>
- <div class="col-sm-7">
- <input type="file" name="file" id="file" class="form-control" />
- </div>
- </div>
- </div>
- <div class="form-group">
- <label for="l" class="col-sm-2 control-label">Nama Tabel</label>
- <div class="col-sm-5">
- <input type="text" name="tabel" class="form-control" value="" placeholder="Nama Tabel" required="required">
- </div>
- </div>
- <div class="form-group">
- <label for="l" class="col-sm-2 control-label"> </label>
- <div class="col-sm-4">
- <button type="submit" class="btn btn-primary"><li class="fa fa-save"> Import</li></button>
- </div>
- </div>
- </form>
- // Controller //
- ///////////////////////////
- //export.php
- <?php
- require_once APPPATH."/libraries/PHPExcel".EXT;
- class Export extends CI_Controller
- {
- function index()
- {
- $this->load->view('uploadexcel');
- }
- function exportapply()
- {
- $file = explode('.',$_FILES['file']['name']);
- $length = count($file);
- if($file[$length -1] == 'xlsx' || $file[$length -1] == 'xls'){
- $tmp = $_FILES['file']['tmp_name'];
- $this->load->library('phpexcel');
- $read = PHPExcel_IOFactory::createReaderForFile($tmp);
- $read->setReadDataOnly(true);
- $excel = $read->load($tmp);
- $sheets = $read->listWorksheetNames($tmp);
- $tabel=$this->input->post('tabel');
- foreach($sheets as $sheet){
- // if($this->db->table_exists($sheet)){
- $_sheet = $excel->setActiveSheetIndexByName($sheet);
- $maxRow = $_sheet->getHighestRow();
- $maxCol = $_sheet->getHighestColumn();
- $field = array();
- $sql = array();
- $maxCol = range('A',$maxCol);
- foreach($maxCol as $key => $coloumn){
- $field[$key] = $_sheet->getCell($coloumn.'1')->getCalculatedValue();
- }
- for($i = 2; $i <= $maxRow; $i++){
- foreach($maxCol as $k => $coloumn){
- $sql[$field[$k]] = $_sheet->getCell($coloumn.$i)->getCalculatedValue();
- }
- $this->db->insert($tabel,$sql);
- }
- // }else{
- // exit('Table tidak valid');
- // }
- }
- exit('Import Sukses')
- }else{
- exit('do not allowed to upload');
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement