Search code examples
phpcodeigniterimport-from-excel

Read .xlsx and .xls data in codeigniter


I want to read the data of .xlsx or .xls file in codeigniter. I have read the other questions related it but nothing works. I have used phpexcel, reader but with no luck. In my project i give the option to upload excel file then i want to read the data and insert it in the database.

Now i am using phpExcel library I have wrote:

    $this->load->library('excel');
    $reader= PHPExcel_IOFactory::createReader('Excel2007');
    $reader->setReadDataOnly(true);
    $path=(FCPATH.'uploads/productfile/'.$_FILES['upload_file']['name']);
    $excel=$reader->load($path);
    $sheet=$excel->setActiveSheetIndex(0);
    for($i=0;$i<=1000;$i++)
    {
      $col1= $sheet->getCellByColumnAndRow(0,$i)->getValue();
      $col2= $sheet->getCellByColumnAndRow(1,$i)->getValue();
      $col3= $sheet->getCellByColumnAndRow(2,$i)->getValue();
      var_dump($col1);
    }

but it display :

Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0 Please give me some example code.

The error enter image description here Please give me some example code:


Solution

  • Thanks to all for your suggestion: I got the solution :

    $file_data = $this->upload->data();
    $file_path =  './uploads/productfile/'.$file_data['file_name'];
    include 'Classes/PHPExcel/IOFactory.php';
    $inputFileName = $file_path; 
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
    $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
    $arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
    for($i=2;$i<=$arrayCount;$i++)
    {                   
        'product'=$allDataInSheet[$i]["C"],
        'brand'=$allDataInSheet[$i]["I"],
        'standard'=$allDataInSheet[$i]["J"],
    }