import data from excel sheet using codeigniter -
i want import sing column of excel sheet database table name admin_record , excel sheet consist of 1 column , name of column bar codes , rows can many.
i want save in database table name admin_record , consists of 2 fields
- id (which primary key)
- bar_code.
find code 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 $table ("; for($index = 1;$index <= $data->sheets[0]['numcols']; $index++){ $sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", "; } $sql = rtrim($sql, ", ")." ) values ( "; ($i = 2; $i <= $data->sheets[0]['numrows']; $i++) { $valuessql = ''; ($j = 1; $j <= $data->sheets[0]['numcols']; $j++) { $valuessql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", "; } echo $sql . rtrim($valuessql, ", ")." ) <br>"; } }
your library file wrong please use following link 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 $table ("; for($index = 1;$index <= $data->sheets[0]['numcols']; $index++){ $sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", "; } $sql = rtrim($sql, ", ")." ) values ( "; ($i = 2; $i <= $data->sheets[0]['numrows']; $i++) { $valuessql = ''; ($j = 1; $j <= $data->sheets[0]['numcols']; $j++) { $valuessql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", "; } echo $sql . rtrim($valuessql, ", ")." ) <br>"; } // add line }
Comments
Post a Comment