Class 'PhpOffice\PhpSpreadsheet\Reader\Xls' not found pada framework Codeigniter

Saya membuat fungsi upload file excel. Pada saat mencoba proses upload, muncul error:

An uncaught Exception was encountered

Type: Error

Message: Class 'PhpOffice\PhpSpreadsheet\Reader\Xls' not found

Filename: C:\xampp-7.4.16\htdocs\webaplikasi\application\controllers\menu_admin.php

Line Number: 657

Backtrace:

File: C:\xampp-7.4.16\htdocs\webaplikasi\index.php

Line: 315

Function: require_once

Saya sudah memasang library PHPSpreadsheet.

Controller:

public function upload_dummy()
        {
            $data = array();
            //$data['title'] = 'Import Excel Sheet | Indo Asia';
            //$data['breadcrumbs'] = array('Home' => '#');

            // Load form validation library
            $this->load->library('form_validation');
            $this->form_validation->set_rules('fileURL', 'Upload File', 'callback_checkFileValidation');
            if($this->form_validation->run() == false) {

                $this->load->view('server_admin/db1/import_statistic/dummy_lokal', $data);
            } else {
                // If file uploaded
                if(!empty($_FILES['fileURL']['name'])) {
                    // get file extension
                    $extension = pathinfo($_FILES['fileURL']['name'], PATHINFO_EXTENSION);

                    if($extension == 'xls'){
                        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
                    } elseif($extension == 'xlsx') {
                        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
                    } else {
                        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
                    }
                    // file path
                    $spreadsheet = $reader->load($_FILES['fileURL']['tmp_name']);
                    $allDataInSheet = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);

                    // array Count
                    $arrayCount = count($allDataInSheet);
                    $flag = 0;
                    $createArray = array(
                        'NoPengajuan',
                        'TanggalPIB',
                        'TanggalBLAWB',
                        'PelabuhanAsal',
                        'PelabuhanTujuan',
                        'Importir',
                        'AlamatImportir',
                        'PPJK',
                        'AlamatPPJK',
                        'Pemasok',
                        'AlamatPemasok',
                        'NegaraPemasok',
                        'HS',
                        'UraianBarang',
                        'Quantity',
                        'KodeSatuan',
                        'Berat',
                        'HargaCIF',
                        'MataUang',
                        'NPWP'
                    );
                    $makeArray = array(
                        'NoPengajuan'       => 'NoPengajuan',
                        'TanggalPIB'        => 'TanggalPIB',
                        'TanggalBLAWB'      => 'TanggalBLAWB',
                        'PelabuhanAsal'     => 'PelabuhanAsal',
                        'PelabuhanTujuan'   => 'PelabuhanTujuan',
                        'Importir'          => 'Importir',
                        'AlamatImportir'    => 'AlamatImportir',
                        'PPJK'              => 'PPJK',
                        'AlamatPPJK'        => 'AlamatPPJK',
                        'Pemasok'           => 'Pemasok',
                        'AlamatPemasok'     => 'AlamatPemasok',
                        'NegaraPemasok'     => 'NegaraPemasok',
                        'HS'                => 'HS',
                        'UraianBarang'      => 'UraianBarang',
                        'Quantity'          => 'Quantity',
                        'KodeSatuan'        => 'KodeSatuan',
                        'Berat'             => 'Berat',
                        'HargaCIF'          => 'HargaCIF',
                        'MataUang'          => 'MataUang',
                        'NPWP'              => 'NPWP'
                    );
                    $SheetDataKey = array();
                    foreach ($allDataInSheet as $dataInSheet) {
                        foreach ($dataInSheet as $key => $value) {
                            if (in_array(trim($value), $createArray)) {
                                $value = preg_replace('/\s+/', '', $value);
                                $SheetDataKey[trim($value)] = $key;
                            }
                        }
                    }
                    $dataDiff = array_diff_key($makeArray, $SheetDataKey);
                    if (empty($dataDiff)) {
                        $flag = 1;
                    }
                    // match excel sheet column
                    if ($flag == 1) {
                        for ($i = 2; $i <= $arrayCount; $i++) {
                            $addresses = array();
                            $NoPengajuan        = $SheetDataKey['NoPengajuan'];
                            $TanggalPIB         = $SheetDataKey['TanggalPIB'];
                            $TanggalBLAWB       = $SheetDataKey['TanggalBLAWB'];
                            $PelabuhanAsal      = $SheetDataKey['PelabuhanAsal'];
                            $PelabuhanTujuan    = $SheetDataKey['PelabuhanTujuan'];
                            $Importir           = $SheetDataKey['Importir'];
                            $AlamatImportir     = $SheetDataKey['AlamatImportir'];
                            $PPJK               = $SheetDataKey['PPJK'];
                            $AlamatPPJK         = $SheetDataKey['AlamatPPJK'];
                            $Pemasok            = $SheetDataKey['Pemasok'];
                            $AlamatPemasok      = $SheetDataKey['AlamatPemasok'];
                            $NegaraPemasok      = $SheetDataKey['NegaraPemasok'];
                            $HS                 = $SheetDataKey['HS'];
                            $UraianBarang       = $SheetDataKey['UraianBarang'];
                            $Quantity           = $SheetDataKey['Quantity'];
                            $KodeSatuan         = $SheetDataKey['KodeSatuan'];
                            $Berat              = $SheetDataKey['Berat'];
                            $HargaCIF           = $SheetDataKey['HargaCIF'];
                            $MataUang           = $SheetDataKey['MataUang'];
                            $NPWP               = $SheetDataKey['NPWP'];

                            $NoPengajuan        = filter_var(trim($allDataInSheet[$i][$NoPengajuan]), FILTER_SANITIZE_STRING);
                            $TanggalPIB         = filter_var(trim($allDataInSheet[$i][$TanggalPIB]), FILTER_SANITIZE_STRING);
                            $TanggalBLAWB       = filter_var(trim($allDataInSheet[$i][$TanggalBLAWB]), FILTER_SANITIZE_EMAIL);
                            $PelabuhanAsal      = filter_var(trim($allDataInSheet[$i][$PelabuhanAsal]), FILTER_SANITIZE_STRING);
                            $PelabuhanTujuan    = filter_var(trim($allDataInSheet[$i][$PelabuhanTujuan]), FILTER_SANITIZE_STRING);
                            $Importir           = filter_var(trim($allDataInSheet[$i][$Importir]), FILTER_SANITIZE_STRING);
                            $AlamatImportir     = filter_var(trim($allDataInSheet[$i][$AlamatImportir]), FILTER_SANITIZE_STRING);
                            $PPJK               = filter_var(trim($allDataInSheet[$i][$PPJK]), FILTER_SANITIZE_STRING);
                            $AlamatPPJK         = filter_var(trim($allDataInSheet[$i][$AlamatPPJK]), FILTER_SANITIZE_STRING);
                            $Pemasok            = filter_var(trim($allDataInSheet[$i][$Pemasok]), FILTER_SANITIZE_STRING);
                            $AlamatPemasok      = filter_var(trim($allDataInSheet[$i][$AlamatPemasok]), FILTER_SANITIZE_STRING);
                            $NegaraPemasok      = filter_var(trim($allDataInSheet[$i][$NegaraPemasok]), FILTER_SANITIZE_STRING);
                            $HS                 = filter_var(trim($allDataInSheet[$i][$HS]), FILTER_SANITIZE_STRING);
                            $UraianBarang       = filter_var(trim($allDataInSheet[$i][$UraianBarang]), FILTER_SANITIZE_STRING);
                            $Quantity           = filter_var(trim($allDataInSheet[$i][$Quantity]), FILTER_SANITIZE_STRING);
                            $KodeSatuan         = filter_var(trim($allDataInSheet[$i][$KodeSatuan]), FILTER_SANITIZE_STRING);
                            $Berat              = filter_var(trim($allDataInSheet[$i][$Berat]), FILTER_SANITIZE_STRING);
                            $HargaCIF           = filter_var(trim($allDataInSheet[$i][$HargaCIF]), FILTER_SANITIZE_STRING);
                            $MataUang           = filter_var(trim($allDataInSheet[$i][$MataUang]), FILTER_SANITIZE_STRING);
                            $NPWP               = filter_var(trim($allDataInSheet[$i][$NPWP]), FILTER_SANITIZE_STRING);

                            //fungsi untuk merubah tipe data datetime ke string/varchar
                            $newTanggalPIB = date("d/m/Y", strtotime($TanggalPIB));
                            $newTanggalBLAWB = date("d/m/Y", strtotime($TanggalBLAWB));

                            $fetchData[] = array(
                                'NoPengajuan'       => $NoPengajuan,
                                'TanggalPIB'        => $newTanggalPIB,
                                'TanggalBLAWB'      => $newTanggalBLAWB,
                                'PelabuhanAsal'     => $PelabuhanAsal,
                                'PelabuhanTujuan'   => $PelabuhanTujuan,
                                'Importir'          => $Importir,
                                'AlamatImportir'    => $AlamatImportir,
                                'PPJK'              => $PPJK,
                                'AlamatPPJK'        => $AlamatPPJK,
                                'Pemasok'           => $Pemasok,
                                'AlamatPemasok'     => $AlamatPemasok,
                                'NegaraPemasok'     => $NegaraPemasok,
                                'HS'                => $HS,
                                'UraianBarang'      => $UraianBarang,
                                'Quantity'          => floatval($Quantity),
                                'KodeSatuan'        => $KodeSatuan,
                                'Berat'             => floatval($Berat),
                                'HargaCIF'          => floatval($HargaCIF),
                                'MataUang'          => $MataUang,
                                'NPWP'              => $NPWP
                            );
                        }
                        $data['dataInfo'] = $fetchData;
                        $this->upload_dummy_m->setBatchImport($fetchData);
                        $this->upload_dummy_m->importData();
                    } else {
                        echo "Please import correct file, did not match excel sheet column";
                    }
                    $this->load->view('server_admin/db1/import_statistic/preview_dummy_lokal', $data);
                }
            }
        }

        // checkFileValidation
        public function checkFileValidation($string) {
            $file_mimes = array('text/x-comma-separated-values',
              'text/comma-separated-values',
              'application/octet-stream',
              'application/vnd.ms-excel',
              'application/x-csv',
              'text/x-csv',
              'text/csv',
              'application/csv',
              'application/excel',
              'application/vnd.msexcel',
              'text/plain',
              'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            );
            if(isset($_FILES['fileURL']['name'])) {
                  $arr_file = explode('.', $_FILES['fileURL']['name']);
                  $extension = end($arr_file);
                  if(($extension == 'xlsx' || $extension == 'xls' || $extension == 'csv') && in_array($_FILES['fileURL']['type'], $file_mimes)){
                      return true;
                  }else{
                      $this->form_validation->set_message('checkFileValidation', 'Please choose correct file.');
                      return false;
                  }
              }else{
                  $this->form_validation->set_message('checkFileValidation', 'Please choose a file.');
                  return false;
              }
          }

Model:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_dummy_m extends CI_Model{
    private $_batchImport;

    public function setBatchImport($batchImport) {
        $this->_batchImport = $batchImport;
    }

    // save data
    public function importData() {
        $data = $this->_batchImport;
        $this->db->insert_batch('dummy', $data);
    }
    // get data list
    public function dataList() {
        $this->db->select(array(
            'd.NoPengajuan',
            'd.TanggalPIB',
            'd.TanggalBLAWB',
            'd.PelabuhanAsal',
            'd.PelabuhanTujuan',
            'd.Importir',
            'd.AlamatImportir',
            'd.PPJK',
            'd.AlamatPPJK',
            'd.Pemasok',
            'd.AlamatPemasok',
            'd.NegaraPemasok',
            'd.HS',
            'd.UraianBarang',
            'd.Quantity',
            'd.KodeSatuan',
            'd.Berat',
            'd.HargaCIF',
            'd.MataUang',
            'd.NPWP'
        ));
        $this->db->from('dummy as d');
        $query = $this->db->get();
        return $query->result_array();
    }
}

View (setelah proses upload, akan redirect ke view ini):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Web | Tabel Dummy</title>
      <?php include 'application/views/komponen/header.php'; //meta, css, js ?>
  </head>
<body>
  <?php
  if ($this->session->userdata('level') == 'administrator') {
    include 'application/views/komponen/navbar_admin.php'; //navbar
  }
  elseif ($this->session->userdata('level') == 'manager') {
    include 'application/views/komponen/navbar_manager.php'; //navbar
  }
  elseif ($this->session->userdata('level') == 'staff') {
    include 'application/views/komponen/navbar_staff.php'; //navbar
  }
  ?>

  <main class="container" role="main">
      <div class="row">
        <div class="col">
          <h2><i class="fas fa-table"></i>Hasil Upload Tabel Dummy</h2>
        </div>
        <div class="col text-right">
          <!--<a href="<?php if($this->session->userdata('level') == 'administrator' || $this->session->userdata('level') == 'manager') { base_url(); } ?>import_statistic" type="button" class="btn btn-outline-primary"><i class="far fa-arrow-alt-circle-left"></i> Kembali</a>-->
          <!-- tombol kembali ketika pindah halaman pagination bug error Message: A non-numeric value encountered -->
        </div>
      </div>
      <hr>
      <div class="alert alert-warning" role="alert">
        <i class="fas fa-exclamation-circle"></i> Fitur ini hanya ditujukan untuk pengembangan data
      </div>
      <div class="row">
        <form class="form" id="search" action="<?php if($this->session->userdata('level') == 'administrator' || $this->session->userdata('level') == 'manager') { base_url(); } ?>cari_master_imp_lokal" method="get">
          <?php include 'application/views/server_admin/db1/import_statistic/komponen/form_cari.php'; ?>
        </form>
        <form class="form" id="search" action="<?php if($this->session->userdata('level') == 'administrator' || $this->session->userdata('level') == 'manager') { base_url(); } ?>laporan_master_imp_lokal" method="post">
          <?php include 'application/views/server_admin/db1/import_statistic/komponen/form_laporan.php'; ?>
        </form>
        <form class="form" id="search" action="<?php if($this->session->userdata('level') == 'administrator' || $this->session->userdata('level') == 'manager') { base_url(); } ?>chart_master_imp_lokal" method="post">
          <?php include 'application/views/server_admin/db1/import_statistic/komponen/form_grafik.php'; ?>
        </form>
        <form class="form" id="search" action="<?php if($this->session->userdata('level') == 'administrator' || $this->session->userdata('level') == 'manager') { base_url(); } ?>upload_dummy" method="post" enctype="multipart/form-data" accept-charset="utf-8">
          <?php include 'application/views/server_admin/db1/import_statistic/komponen/form_upload.php'; ?>
        </form>
      </div>
      <br />
      <div class="table-responsive">
      <table class="table table-striped">
          <thead>
            <tr class="table-primary">
              <th scope="col">NoPengajuan</th>
              <th scope="col">TanggalPIB</th>
              <th scope="col">TanggalBLAWB</th>
              <th scope="col">PelabuhanAsal</th>
              <th scope="col">PelabuhanTujuan</th>
              <th scope="col">Importir</th>
              <th scope="col">AlamatImportir</th>
              <th scope="col">PPJK</th>
              <th scope="col">AlamatPPJK</th>
              <th scope="col">Pemasok</th>
              <th scope="col">AlamatPemasok</th>
              <th scope="col">NegaraPemasok</th>
              <th scope="col">HS</th>
              <th scope="col">UraianBarang</th>
              <th scope="col">Quantity</th>
              <th scope="col">KodeSatuan</th>
              <th scope="col">Berat</th>
              <th scope="col">HargaCIF</th>
              <th scope="col">MataUang</th>
              <th scope="col">NPWP</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach($dataInfo as $key=>$element) { ?>
            <tr>
              <td><?php print $element['NoPengajuan'];?></td>
              <td><?php print $element['TanggalPIB'];?></td>
              <td><?php print $element['TanggalBLAWB'];?></td>
              <td><?php print $element['PelabuhanAsal'];?></td>
              <td><?php print $element['PelabuhanTujuan'];?></td>
              <td><?php print $element['Importir'];?></td>
              <td><?php print $element['AlamatImportir'];?></td>
              <td><?php print $element['PPJK'];?></td>
              <td><?php print $element['AlamatPPJK'];?></td>
              <td><?php print $element['Pemasok'];?></td>
              <td><?php print $element['AlamatPemasok'];?></td>
              <td><?php print $element['NegaraPemasok'];?></td>
              <td><?php print $element['HS'];?></td>
              <td><?php print $element['UraianBarang'];?></td>
              <td><?php print $element['Quantity'];?></td>
              <td><?php print $element['KodeSatuan'];?></td>
              <td><?php print $element['Berat'];?></td>
              <td><?php print $element['HargaCIF'];?></td>
              <td><?php print $element['MataUang'];?></td>
              <td><?php print $element['NPWP'];?></td>
            </tr>
          <?php } ?>
          </tbody>
        </table>
      </div>
  </main>
  <?php include 'application/views/komponen/footer.php'; //footer ?>
</body>
</html>

mohon bantuannya, terima kasih.

avatar IndoCode
@IndoCode

11 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

<div>Pastikan kamu sudah mengunduh library PhpSpreadsheetnya ya, kamu bisa install librarynya menggunakan composer dengan command berikut :</div><pre>composer require phpoffice/phpspreadsheet</pre><div><br>setelah itu, ketika kamu akan menggunakan fungsi dari PhpSpreadsheet, jangan lupa import class-nya dengan menggunakan namespace, berikut contoh sederhananya :<br><br></div><pre><strong>&lt;?php</strong> //Memanggil file autoload <strong>require</strong> 'vendor/autoload.php';

//Memanggil class dari PhpSpreadsheet dengan namespace <strong>use</strong> <strong>PhpOffice</strong>&lt;strong>PhpSpreadsheet</strong>&lt;strong>Spreadsheet</strong>; <strong>use</strong> <strong>PhpOffice</strong>&lt;strong>PhpSpreadsheet</strong>&lt;strong>Writer</strong>&lt;strong>Xlsx</strong>;

$spreadsheet = <strong>new</strong> Spreadsheet(); $sheet = $spreadsheet-&gt;getActiveSheet(); $sheet-&gt;setCellValue('A1', 'Hello World !');

$writer = <strong>new</strong> Xlsx($spreadsheet); $writer-&gt;save('hello world.xlsx');</pre><div><br>Untuk me-load library yang diinstall via Composer di framework Codeigniter, ubah file config yang berada pada folder application/config/config.php sebagai berikut :</div><pre>Dari $config['composer_autoload'] = FALSE;

diubah menjadi: $config['composer_autoload'] = './vendor/autoload.php'; <br></pre><div>Semoga berhasil.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Tanggapan

untuk $config['composer_autoload'] = './vendor/autoload.php'; apakah boleh menggunakan $config['composer_autoload'] = 'TRUE'; ? setau saya $config['composer_autoload'] berfungsi untuk mengaktifkan perintah composer ke dalam codeigniter

Login untuk ikut Jawaban