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>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看