0

Trying to import XLS file to my mysql table. But returns error below.

excelimport/storage/framework/laravel-excel/laravel-excel-BBA94ICWI0E7wrAuLevn89J6OElWa1qR.xls is not recognised as an OLE file

Here's my code. I know that, the file got wrong format, but when I save as an excel (without changing anything), It works well.

But problem is; my users will upload files like that. And I need to solve it without saying "please export your xls file as excel again".

$file = $request->file('file')->store('import');
Excel::import(new UserImportModel, $file);
TCS
  • 629
  • 3
  • 11
  • 32
  • Any chance the following helps? https://stackoverflow.com/questions/28688652/the-filename-062014-xlsx-is-not-recognised-as-an-ole-file – Altimus Prime Jan 31 '21 at 15:14
  • `$inputFileType = PHPExcel_IOFactory::identify($file); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objReader->setReadDataOnly(false); $objPHPExcel = $objReader->load($file);` – Altimus Prime Jan 31 '21 at 15:17

1 Answers1

0

You can try this pattern, this works for my codes

<?php
    require 'vendor1/autoload.php';
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    use PhpOffice\PhpSpreadsheet\IOFactory;
    use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
    use PhpOffice\PhpSpreadsheet\Writer\Pdf;
    $excelreader = IOFactory::createReader('Xml');
    $spreadsheet = new Spreadsheet();
    $writer = new Xlsx($spreadsheet);
    $writer->setPreCalculateFormulas(false);
    //
    if(isset($_POST['import'])){
        $hapus = mysqli_query($bestdt,"DELETE FROM 2023_pdkeluaroke WHERE npsn = '$npsn' ");
        $excelnya = 'siswakeluar'.$npsn.'.xls';
        $loadexcel = $excelreader->load('tmp/'.$excelnya);

        //HAL 1
        $sheet = $loadexcel->setActiveSheetIndex(0); 
        $sheet = $loadexcel->getActiveSheet(0)->toArray(null, true, true ,true);
        $numrow = 1;
        $kosong = ' ';
        $no= 0;
        foreach($sheet as $row){
            $nipd = mysqli_real_escape_string($bestdt,$row['A']);
            $nama_siswa = mysqli_real_escape_string($bestdt,$row['B']);
            $jk = mysqli_real_escape_string($bestdt,$row['C']);
            $nisn = mysqli_real_escape_string($bestdt,$row['D']);
            $tempat_lahir = mysqli_real_escape_string($bestdt,$row['E']);
            $tanggal_lahir = mysqli_real_escape_string($bestdt,$row['F']);
            $nik = mysqli_real_escape_string($bestdt,$row['G']);
            $agama = mysqli_real_escape_string($bestdt,$row['H']);

            $alamat = mysqli_real_escape_string($bestdt,$row['J']);
            $rt = mysqli_real_escape_string($bestdt,$row['K']);
            $rw = mysqli_real_escape_string($bestdt,$row['L']);
            $dusun = mysqli_real_escape_string($bestdt,$row['M']);
            $kelurahan = mysqli_real_escape_string($bestdt,$row['N']);
            $kecamatan = mysqli_real_escape_string($bestdt,$row['O']);

            $kode_post = mysqli_real_escape_string($bestdt,$row['P']);
            $tinggal = mysqli_real_escape_string($bestdt,$row['Q']);
            $transportasi = mysqli_real_escape_string($bestdt,$row['R']);
            $telepone = mysqli_real_escape_string($bestdt,$row['S']);
            $hp = mysqli_real_escape_string($bestdt,$row['T']);
            $email = mysqli_real_escape_string($bestdt,$row['U']);
            $skhun = mysqli_real_escape_string($bestdt,$row['P']);
            $kps = mysqli_real_escape_string($bestdt,$row['W']);

            $no_kps = mysqli_real_escape_string($bestdt,$row['X']);
            $nama_ayah = mysqli_real_escape_string($bestdt,$row['Y']);
            $thnlhr_ayah = mysqli_real_escape_string($bestdt,$row['Z']);
            $sekolah_ayah = mysqli_real_escape_string($bestdt,$row['AA']);
            $kerja_ayah = mysqli_real_escape_string($bestdt,$row['AB']);
            $hasil_ayah = mysqli_real_escape_string($bestdt,$row['AC']);
            $nik_ayah = mysqli_real_escape_string($bestdt,$row['AD']);

            $nama_ibu = mysqli_real_escape_string($bestdt,$row['AE']);
            $thnlhr_ibu = mysqli_real_escape_string($bestdt,$row['AF']);
            $sekolah_ibu = mysqli_real_escape_string($bestdt,$row['AG']);
            $kerja_ibu = mysqli_real_escape_string($bestdt,$row['AH']);
            $hasil_ibu = mysqli_real_escape_string($bestdt,$row['AI']);
            $nik_ibu = mysqli_real_escape_string($bestdt,$row['AJ']);
            $nama_wali = mysqli_real_escape_string($bestdt,$row['AK']);
            $thnlhr_wali = mysqli_real_escape_string($bestdt,$row['AL']);
            $sekolah_wali = mysqli_real_escape_string($bestdt,$row['AM']);
            $kerja_wali = mysqli_real_escape_string($bestdt,$row['AN']);
            $hasil_wali = mysqli_real_escape_string($bestdt,$row['AO']);

            $alasan_keluar = mysqli_real_escape_string($bestdt,$row['AP']);
            $tanggal_keluar = mysqli_real_escape_string($bestdt,$row['AQ']);
            //
            if ($tanggal_lahir == ""  )
            continue; 
            if($numrow > 1){
                $no++;
                $query = mysqli_query($bestdt, "INSERT INTO 2023_pdkeluaroke(npsn, nama_siswa, nipd, jk, nisn, tempat_lahir, tanggal_lahir, nik, agama, alamat, rt, rw, dusun, kelurahan, kecamatan, kode_post, tinggal, transportasi, telepone, hp, email, skhun, kps, no_kps, nama_ayah, thnlhr_ayah, sekolah_ayah, kerja_ayah, hasil_ayah, nik_ayah, nama_ibu, thnlhr_ibu, sekolah_ibu, kerja_ibu, hasil_ibu, nik_ibu, nama_wali, thnlhr_wali, sekolah_wali, kerja_wali, hasil_wali, alasan_keluar, tanggal_keluar) VALUES('".$npsn."','".$nama_siswa."','".$nipd."','".$jk."','".$nisn."','".$tempat_lahir."','".$tanggal_lahir."','".$nik."','".$agama."','".$alamat."','".$rt."','".$rw."','".$dusun."','".$kelurahan."','".$kecamatan."','".$kode_post."','".$tinggal."','".$transportasi."','".$telepone."','".$hp."','".$email."','".$skhun."','".$kps."','".$no_kps."','".$nama_ayah."','".$thnlhr_ayah."','".$sekolah_ayah."','".$kerja_ayah."','".$hasil_ayah."','".$nik_ayah."','".$nama_ibu."','".$thnlhr_ibu."','".$sekolah_ibu."','".$kerja_ibu."','".$hasil_ibu."','".$nik_ibu."','".$nama_wali."','".$thnlhr_wali."','".$sekolah_wali."','".$kerja_wali."','".$hasil_wali."','".$alasan_keluar."','".$tanggal_keluar."') "); 
            } $numrow++ ; 
        }
    }
    echo "<script>window.location = '?page=data_siswakeluar' </script>";
?>

Don't forget to connect to your database

Ram Chander
  • 1,088
  • 2
  • 18
  • 36
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – ethry Aug 26 '23 at 21:51