从第2列开始使用excel从数据库插入数据库

时间:2018-10-10 08:17:45

标签: php excel database

我正在尝试从excel文件但从第2列开始将数据插入数据库中,因为第一列被用作表中行的名称。

这是我输入文件的表单,此代码从第一列读取,我想从第二列开始读取。

<h2>Import Excel File into MySQL Database using PHP</h2>
    <div class="outer-container">
        <form action="import.php" method="post"
            name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">
            <div>
                <label>Choose Excel
                    File</label> <input type="file" name="file"
                    id="file" accept=".xls,.xlsx">
                <button type="submit" id="submit" name="import"
                    class="btn-submit">Import</button>

            </div>

        </form>

    </div>
    <div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>

这是动作文件:

<?php
include 'koneksi/koneksi.php';
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');

if (isset($_POST["import"]))
{

  $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];

  if(in_array($_FILES["file"]["type"],$allowedFileType)){

        $targetPath = 'uploads/'.$_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);

        $Reader = new SpreadsheetReader($targetPath);

        $sheetCount = count($Reader->sheets());

        for($i=0;$i<$sheetCount;$i++)
        {
            $Reader->ChangeSheet($i);

            foreach ($Reader as $Row )
            {

                $id_koperasi = "";
                if(isset($Row[0])) {
                    $id_koperasi = mysqli_real_escape_string($con,$Row[0]);
                }

                $nama_koperasi = "";
                if(isset($Row[1])) {
                    $nama_koperasi = mysqli_real_escape_string($con,$Row[1]);
                }

                $alamat = "";
                if(isset($Row[2])) {
                    $alamat = mysqli_real_escape_string($con,$Row[2]);
                }

                $telp = "";
                if(isset($Row[3])) {
                    $telp = mysqli_real_escape_string($con,$Row[3]);
                }

                $hp = "";
                if(isset($Row[4])) {
                    $hp = mysqli_real_escape_string($con,$Row[4]);
                }

                $nama_cp = "";
                if(isset($Row[5])) {
                    $nama_cp = mysqli_real_escape_string($con,$Row[5]);
                }

                $email = "";
                if(isset($Row[6])) {
                    $email = mysqli_real_escape_string($con,$Row[6]);
                }

                $tanggal_fu = "";
                if(isset($Row[7])) {
                    $tanggal_fu = mysqli_real_escape_string($con,$Row[7]);
                }

                $ket_fu = "";
                if(isset($Row[8])) {
                    $ket_fu = mysqli_real_escape_string($con,$Row[8]);
                }

                $hasil_pembahasan = "";
                if(isset($Row[9])) {
                    $hasil_pembahasan = mysqli_real_escape_string($con,$Row[9]);
                }

                $status = "";
                if(isset($Row[10])) {
                    $status = mysqli_real_escape_string($con,$Row[10]);
                }

                $provinsi = "";
                if(isset($Row[11])) {
                    $provinsi = mysqli_real_escape_string($con,$Row[11]);
                }

                $kota = "";
                if(isset($Row[12])) {
                    $kota = mysqli_real_escape_string($con,$Row[12]);
                }

                $kec = "";
                if(isset($Row[13])) {
                    $kec = mysqli_real_escape_string($con,$Row[13]);
                }

                $kel = "";
                if(isset($Row[14])) {
                    $kel = mysqli_real_escape_string($con,$Row[14]);
                }

                $rt = "";
                if(isset($Row[15])) {
                    $rt = mysqli_real_escape_string($con,$Row[15]);
                }

                $rw = "";
                if(isset($Row[16])) {
                    $rw = mysqli_real_escape_string($con,$Row[16]);
                }

                $jln = "";
                if(isset($Row[17])) {
                    $jln = mysqli_real_escape_string($con,$Row[17]);
                }

                $kodep = "";
                if(isset($Row[18])) {
                    $kodep = mysqli_real_escape_string($con,$Row[18]);
                }

                if (!empty($id_koperasi) || !empty($nama_koperasi) || !empty($alamat) || !empty($telp) || !empty($hp) || !empty($nama_cp) || !empty($email) || !empty($tanggal_fu) || !empty($ket_fu) || !empty($hasil_pembahasan) || !empty($status) || !empty($provinsi) || !empty($kota) || !empty($kec) || !empty($kel) || !empty($rt) || !empty($rw) || !empty($jln) || !empty($kodep) ) {
                    $query = "INSERT INTO t_koperasi(id,id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status,provinsi,kota,kec,kel,rt,rw,jln,kodep) VALUES ('',
        '$id_koperasi',
        '$nama_koperasi',
        '$alamat',
        '$telp',
        '$hp',
        '$nama_cp',
        '$email',
        '$tanggal_fu',
        '$ket_fu',
        '$hasil_pembahasan',
        '$status',
        '$provinsi',
        '$kota',
        '$kec',
        '$kel',
        '$rt',
        '$rw',
        '$jln',
        '$kodep')" or die(mysqli_error($con));
    ;
                    $result = mysqli_query($con, $query);

                    if (! empty($result)) {
                        $type = "success";
                        $message = "SUKSES";
                    } else {
                        $type = "error";
                        $message = "Problem in Importing Excel Data";

                    }
                }
             }

         }
  }
  else
  { 
        $type = "error";
        $message = "Invalid File Type. Upload Excel File.";
  }
}

include 'views/v_import.php'
?>

我已经尝试过将$ col = 1放置了,但仍然无法使用,我尝试使用for=i,但仍然无法使用。

1 个答案:

答案 0 :(得分:0)

带有额外计数器的可能解决方案

        $Reader->ChangeSheet($i);
        $number = 0;
        foreach ($Reader as $Row )
        {
            if ($number!=0) {
               ......
            }
            $number++;
        }