使用PHP在一个MySQL表列中插入两个Excel列

时间:2018-03-21 08:54:01

标签: php mysql excel phpexcel

我有一个包含此格式的Excel文件:

  

lon lat site1 site2

     

x,x1 y,y1 name1 name2

     

x,x2 y,y2

     

x,x3 y,y3 name3 name4

     

x,x4 y,y4

等等, 我需要知道如何将它插入MySQL表中,就像这种形式一样

  

cord1 cord2 site1 site2

     

x,x1 / y,y1 x,x2 / y,y2 name1 name2

     

x,x3 / y,y3 x,x4 / y,y4 name3 name4

等等, 这是我使用的代码:

<?php
$hostname = "localhost";
$database = "DB";
$username = "root";
$password = "";
@$conexion = mysql_connect($hostname, $username, $password) or                 trigger_error(mysql_error(),E_USER_ERROR);  
mysql_select_db($database, $conexion);
require_once 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
$inputFileName ='FILE.xlsx'; 
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName); // télécherger le fichier 
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'":   '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();
$i=0 ;
$j=1 ;
for ($row = 2; $row <= $highestRow; $row++){ 
    $rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn .        $row,NULL,TRUE,FALSE);
}
foreach($rowData as $row) {
foreach($row[0] as $var) {  
 $t[$i]= $var ;
 $i = $i +1 ;   
}   
for($a=1;$a<= $highestRow ;$a+=2){
    $lan1[$a]=$t[0] ;    
    $long1[$a]=$t[1] ;
    $coor1[$i]=$lan1[$a].",".$long1[$a]; 
    } 
for($b=2;$b<= $highestRow ;$b+=2){
    $lan2[$b]=$t[0] ;    
    $long2[$b]=$t[1] ;  
    $coor2[$i]=$lan2[$b].",".$long2[$b]; 
    }
$sql= " INSERT INTO  line    (`coordinates1`,`coordinates2`,`name1`,`name2`,`color`,`interface1`,`interface2`    )         VALUES     ('".utf8_decode($coor1[$i])."','".utf8_decode($coor2[$i])."','".utf8_decode($name1)."','".utf8_decode($name2)."','".utf8_decode($t[3])."','".utf8_decode($interface1)."','".utf8_decode($interface2)."')" ; 
$res=mysql_query($sql, $conexion) or die(mysql_error());
$sql2= " DELETE FROM line WHERE name1=''" ; 
$res2=mysql_query($sql2, $conexion) or die(mysql_error());   
 }} 
$i = 0 ;
}
?>    

变量的名称不一样。拜托,我真的需要你的帮助。感谢。

0 个答案:

没有答案