1

我想将excel表的单列导入数据库表名admin_record,excel表只包含一列,列名是条形码,行可以很多。

我想将它保存在数据库中,我的表名是 admin_record,它只包含两个字段

  1. id(主键)
  2. 条码。

只找到我完全无法理解的代码

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>";
    }
}
4

1 回答 1

1

您的库文件错误,请使用以下链接下载 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


    }
于 2015-02-10T12:11:43.680 回答