Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // memanggil koneksi
- include "pengaturan/koneksi.php";
- // memanggil class PHPExcel
- require_once "PHPExcel-1.8/Classes/PHPExcel.php";
- require_once "PHPExcel-1.8/Classes/PHPExcel/IOFactory.php";
- // load excel
- $file = $_FILES['excel']['tmp_name'];
- $inputFileName = 'import.xlsx';
- //pindahkan ke tempat yang bisa di baca
- copy($file, $inputFileName);
- //tipe dari file
- $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
- //$load = PHPExcel_IOFactory::load($file);
- //Load file
- $objReader = PHPExcel_IOFactory::createReader($inputFileType);
- $objPHPExcel = $objReader->load($inputFileName);
- //$sheets = $load->getActiveSheet()->toArray(null,true,true,true);
- $tgl_sekarang = date("Ymd");
- $i = 1;
- $err=false;
- //foreach ($sheets as $sheet)
- foreach( $objPHPExcel->getWorksheetIterator() as $worksheet )
- {
- // karena data yang di excel di mulai dari baris ke 2
- // maka jika $i lebih dari 1 data akan di masukan ke database
- $n = $worksheet->getHighestRow();
- for($i=2;$i<=$n;$i++)
- {
- //sheet[A]
- $masterID = $worksheet->getCellByColumnAndRow(0, $i)->getValue() ;//A
- //sheet[B]..dst
- $created_at = $worksheet->getCellByColumnAndRow(1, $i)->getValue() ;//B
- $tgl_sekarang = excel2date($created_at);
- $so = $worksheet->getCellByColumnAndRow(2, $i)->getValue() ;
- $wo = $worksheet->getCellByColumnAndRow(3, $i)->getValue() ;
- $buyer = $worksheet->getCellByColumnAndRow(4, $i)->getValue() ;
- $season = $worksheet->getCellByColumnAndRow(5, $i)->getValue() ;
- $style = $worksheet->getCellByColumnAndRow(6, $i)->getValue() ;
- $orderName = $worksheet->getCellByColumnAndRow(7, $i)->getValue() ;
- $color = $worksheet->getCellByColumnAndRow(8, $i)->getValue() ;
- //---------------TOD
- $tod0 = $worksheet->getCellByColumnAndRow(9, $i)->getValue() ;
- $tod = excel2date($tod0);
- $destination = $worksheet->getCellByColumnAndRow(10, $i)->getValue() ;
- $size = $worksheet->getCellByColumnAndRow(11, $i)->getValue() ;
- $orderQty = $worksheet->getCellByColumnAndRow(11, $i)->getValue() ;
- $sql = "INSERT INTO master SET
- masterId='$masterID',
- created_at='$tgl_sekarang',
- so='$so',
- wo='$wo',
- buyer='$buyer',
- season='$season',
- style='$style',
- orderNumber='$orderNumber',
- color='$color',
- tod='$tod',
- destination='$destination',
- size='$size',
- orderQty='$orderQty'";
- $stat = mysql_query($sql);
- if(!$stat)
- {
- //query error?!!
- $err=mysql_error();
- $n=-1;
- //tidak dilanjutkan... langsung stop
- }
- }
- // $i++;
- }
- function excel2date($numDate)
- {
- if($numDate==0||$numDate=='') return NULL;
- $unixDate = ($numDate - 25569) * 86400;
- //return $unixDate;
- return gmdate("Y-m-d H:i:s", $unixDate);
- }
- if($err)
- {
- //bila ada error.. maka akan memberi tahu
- die( 'masih ada error:'.$err."|query:".$sql );
- }
- else
- {
- //tidak ada masalah
- header("Location: " . $_SERVER['HTTP_REFERER']);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement