2011-05-17 96 views
2

正試圖讀取excel文件(即xls)並將數據導入到mysql數據庫並使用codeigniter 2.0.1框架。在php中使用codeigniter和Spreadsheet_Excel_Reader庫讀取xls文件

這裏的代碼生成查詢

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

} 

在迴路中產生的值是重複的,我不能找到與循環問題...

這裏是運行函數後的結果以上:

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 ("[email protected]", "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 ("[email protected]", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", "[email protected]", "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 ("[email protected]", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", "[email protected]", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8", " [email protected]", "2011-04-10 08:21", "Chunyu", "2", "4", "5", "3", "56", "5", "6", "8", "45", "7", "8", "9", "8", "9", "8") 

對不起,我不能發佈圖像,因爲我是一個新手。

感謝名單中advance..Cheers

回答

1

我不能測試,但...:

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>"; 
    } 
} 
+0

感謝名單了很多幫助out..The $ valuesSQL的值,因爲我們分配不同的overwiten價值觀相同的變量,我認爲把他們在一個陣列將是最好的做法..你可以請向我解釋爲什麼代碼失敗的第一個地方?.. thanx你非常保存我的一天.. – 2011-05-17 10:10:08

+0

首先在你的$ sql你保留INSERT INTO ... VALUES(然後你連接到這個字符串的所有行和列,相反,我使用的是每行最初的$ sql,並將它與來自那一排。 – CristiC 2011-05-17 10:37:03