1
votes

I want to import sing column of excel sheet into database table name admin_record and excel sheet is only consist of one column and name of the column is bar codes and rows can be many.

I want to save it in database my table name is admin_record and its consists of only two fields

  1. id (which is primary key)
  2. bar_code.

find only this code which i totally cant understand

public function read_file($table = 'admin_record', $filename = 'example.xls') {

    $pathToFile = 'uploads/' . $filename;
    $this->load->library('Spreadsheet_Excel_Reader');
    $data = new Spreadsheet_Excel_Reader($pathToFile);
    $sql = "INSERT INTO $table (";
    for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
    $sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
    }

    $sql = rtrim($sql, ", ")." ) VALUES ( ";
    for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
        $valuesSQL = '';
    for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
        $valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
    }
    echo $sql . rtrim($valuesSql, ", ")." ) <br>";
    }
}
1
I think ur code will work fine, try to give excel sheet path to variable "$pathToFile" and print the array of "$data".. then send me what result ur getting..!! and most important is u must be included Spreadsheet_Excel_Reader in ur library - jsduniya
it said unable to load library - Arslan Akhtar
please check do u have Spreadsheet_Excel_Reader files in ur library - jsduniya
nope tell me the link plz where in can download excel_reader.php for lib and specific code that save data in sql from excel file.. - Arslan Akhtar
Heres the excel reader lib for CI : github.com/EllisLab/CodeIgniter/wiki/Excel-Reader-Class plz check this link - Syed Ahmed

1 Answers

1
votes

your library file is wrong please use following link to download Spreadsheet_Excel_Reader Library

public function my_test($table = 'admin_record', $filename = 'resources/uploads/c.xls')
    {

           $pathToFile = $filename;
           $valuesSql="";
            $this->load->library('Spreadsheet_Excel_Reader');
            $data = new Spreadsheet_Excel_Reader($pathToFile);
            $sql = "INSERT INTO $table (";
            for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
            $sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
            }

            $sql = rtrim($sql, ", ")." ) VALUES ( ";
            for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
            $valuesSQL = '';
            for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
            $valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
            }
            echo $sql . rtrim($valuesSql, ", ")." ) <br>";
            }                                           // add this line


    }