doushi6864 2011-05-17 09:46
浏览 61
已采纳

使用带有Spreadsheet_Excel_Reader库的codeigniter在php中读取xls文件

am trying to read an excel file (i.e xls) and import the data to mysql database and am using codeigniter 2.0.1 framework.

here are the codes to generate query

    public function read_file($table = 'organization', $filename = 'test.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++) {
        for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
            $sql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
        }
        echo  rtrim($sql, ", ")." ) <br>";
    }

}

the values generated in the loop are duplicated , i cant find the problem with the loop...

Here is the result after running the function above:

INSERT INTO organization (userid, datestamp, kata, kaya, kaya_zenye_vuoo, vyoo_vyenye_bamba, mifuniko, matumizi_mifuniko, uzuiaji_inzi, usafishikaji_sakafu, usitiri, uezekaji, milango_inayofungika, harufu, wadudu, sabuni, maji, vibuyuchirizi ) VALUES ( "mpwapwa@live.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8" ) 
INSERT INTO organization (userid, datestamp, kata, kaya, kaya_zenye_vuoo, vyoo_vyenye_bamba, mifuniko, matumizi_mifuniko, uzuiaji_inzi, usafishikaji_sakafu, usitiri, uezekaji, milango_inayofungika, harufu, wadudu, sabuni, maji, vibuyuchirizi ) VALUES ( "mpwapwa@live.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", "annie@yahoo.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8" ) 
INSERT INTO organization (userid, datestamp, kata, kaya, kaya_zenye_vuoo, vyoo_vyenye_bamba, mifuniko, matumizi_mifuniko, uzuiaji_inzi, usafishikaji_sakafu, usitiri, uezekaji, milango_inayofungika, harufu, wadudu, sabuni, maji, vibuyuchirizi ) VALUES ( "mpwapwa@live.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", "annie@yahoo.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", " sam@yahoo.com", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8" ) 

Sorry i cant post images as i'm a newbie.

Thanx in advance..Cheers

  • 写回答

1条回答 默认 最新

  • doujiang2812 2011-05-17 09:55
    关注

    I cannot test it, but... :

        public function read_file($table = 'organization', $filename = 'test.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>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?