0
votes

I wanted to import xls file in laravel and export the data in database. But I am getting following error while importing

at HandleExceptions->handleError(8, 'Undefined variable: contents', 'C:\Users\Kuldeeo\Downloads\app\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\ZipArchive.php', 174, array('fileName' => '_rels/.rels', 'list' => 0, 'listCount' => 1, 'list_index' => -1, 'i' => 1, 'extracted' => 0, 'filename' => 'rels/.rels'))

I have tried adding

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

in Reader/Excel2007.php and Writer/Excel2007.php

even updated the composer several times. Downloading the xls file is working but importing it, is producing the error. Kindly help.....

1

1 Answers

0
votes

I am using package = "maatwebsite/excel": "^2.1" in laravel to import xls file and store in DB like this I hope it helps you.

Route::get('/route_name', function () {
   \Excel::load("file_path/file_name.xls", function($reader) {
        $sheetTitle = array();
        $results = $reader->all();
        foreach($results as $key=>$sheet)
        {
            $sheetTitle[$key]["id"] = (int)$sheet->id;
            $sheetTitle[$key]["plan_id"] = (int)$sheet->plan_id;
            $sheetTitle[$key]["description"] = $sheet->description;
        }
        \DB::table('table_name')->insert($sheetTitle);
        return dd(\DB::table('table_name')->get());
    });
});

id,plan_id,description are title (first row) of excel file.

Update

Or may be you can use in controller as

Route::post('/route_name', 'UserController@importData');

And in UserController

function importData(Request $request){
     \Excel::load($request->file('excel_file'), function($reader) {
     .....//All code will be same as above.
}