簡體   English   中英

將Excel數據插入Codeigniter中的數據庫

[英]insert excel data into database in codeigniter

我需要使用codeigniter將excel工作表數據插入數據庫中。為此,我嘗試了

<?php
include 'reader.php';
$excel = new Spreadsheet_Excel_Reader();
?>
<table>
<?php
$excel->read('first_data.xls');    
$x=1;
while($x<=$excel->sheets[0]['numRows']) {
  echo "\t<tr>\n";
  $y=1;
  while($y<=$excel->sheets[0]['numCols']) {
    $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
    echo "\t\t<td>$cell</td>\n";  
    $y++;
  }  
  echo "\t</tr>\n";
  $x++;

}

?>    
</table><br/>

通過這種方式,我可以檢索Excel數據並進行打印。 如何在數據庫中一行一行地存儲。

State   1972-1973   1973-1974   1974-1975   1975-1976   1976-1977   1977-1978   1978-1979   1979-1980   1980-1981   1981-1982
Alabama $733,750    $1,066,300  $1,136,244  $1,343,670  $1,476,307  $1,642,927  $1,507,315  $1,849,825  $2,402,873  $2,079,000 
Alaska  $1,019,000  $1,100,000  $1,180,500  $1,172,300  $1,415,300  $1,411,700  $1,666,500  $2,026,400  $3,409,800  $7,200,000 
Arizona $225,117    $226,000    $242,000    $212,200    $553,600    $905,200    $1,100,300  $1,232,600  $1,409,600  $1,557,000 
Arkansas    $890,496    $1,173,304  $1,193,362  $1,735,266  $1,824,536  $1,929,071  $2,090,590  $2,173,595  $2,042,632  $2,203,864 

這個建議可能不在問題范圍之內,但是您可以考慮將excel文件導出到.csv,然后使用mysql導入導入文件嗎?

希望此代碼段對您有所幫助。

if ( file_exists( $file ) )
{
    $fields = $this->db->list_fields($table_name);
    $field_list = implode('`,`',$fields);
    $sql = "LOAD DATA LOCAL INFILE '". $file."'
        REPLACE
        INTO TABLE `$table_name`
        FIELDS terminated BY ','
        OPTIONALLY ENCLOSED BY '\"'
        LINES terminated BY '\\n' IGNORE 1 LINES
        (`${field_list}`); ";
    $this->db->query($sql);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM