2014-09-26 107 views
0

我正在努力找到一個教程,告訴我如何編寫一個php腳本,它可以讓我上傳並將xls(不是CSV)插入到mysql中。我有這個PHP腳本fgetcsv,它的工作都很好。但我需要包含對xls的支持。上傳並插入xls文件到MySQL?

我的PHP

if(isset($_POST["Import"])){ 
    echo $filename=$_FILES["file"]["tmp_name"]; 
    if($_FILES["file"]["size"] > 0) 
    { 
    $file = fopen($filename, "r"); 
    while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) 
    { 
    //It wiil insert a row to our subject table from our csv file` 
      $sql = "INSERT into excell_import (`objekt`, `element_nummer`, `element_hojd`, `element_langd`,COURSE_ID, `AY`, `SEMESTER`) 
       values('$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$em apData[7]')"; 
     //we are using mysql_query function. it returns a resource on true else False on error 
      $result = mysql_query($sql, $conn); 
      if(! $result) 
      { 
       echo "<script type=\"text/javascript\"> 
         alert(\"Invalid File:Please Upload CSV File.\"); 
         window.location = \"index.php\" 
        </script>"; 
      } 
} 
     fclose($file); 
     //throws a message if data successfully imported to mysql database from excel file 
     echo "<script type=\"text/javascript\"> 
        alert(\"CSV File has been successfully Imported.\"); 
        window.location = \"index.php\" 
       </script>"; 
     //close of connection 
     mysql_close($conn); 

}  
?>  
+0

嘗試谷歌的PHP的excel庫,'PHPExcel'例如 – 2014-09-26 11:22:35

+0

所以其中許多PHP庫,其能夠讀取真實如[PHPExcel](https://phpexcel.codeplex.com/)的您實際上看過BIFF格式的xls文件嗎? – 2014-09-26 11:22:56

+0

嘗試谷歌您的需求,有一些圖書館已經存在這個,如https://phpacademy.org/topics/phpexcel-inserting-xlsxxls-to-mysql/31882 – 2014-09-26 11:23:25

回答

1

從xls文件收集數據的工作腳本並插入到MySQL。

<?php 
//include the following 2 files for phpexcel 
require 'Classes/PHPExcel.php'; 
require_once 'Classes/PHPExcel/IOFactory.php'; 

//Establish connection to mysql 
$conn=mysql_connect($host,$username,$password) or die("Could not connect"); 
mysql_select_db($dbname,$conn) or die("could not connect database"); 

//Load file 
$path = "atbl.xls"; 
$objPHPExcel = PHPExcel_IOFactory::load($path); 

//Loop threw file to get data 
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { 
$worksheetTitle  = $worksheet->getTitle(); 
$highestRow   = 20; //$worksheet->getHighestRow(); // e.g. 10 
$highestColumn  = 'G'; //worksheet->getHighestColumn(''); // e.g 'F' 
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
$nrColumns = ord($highestColumn) - 64; 
echo "<br>The worksheet ".$worksheetTitle." has "; 
echo $nrColumns . ' columns (A-' . $highestColumn . ') '; 
echo ' and ' . $highestRow . ' row.'; 
echo '<br>Data: <table border="1"><tr>'; 
for ($row = 11; $row <= $highestRow; ++ $row) { 
    echo '<tr>'; 
    for ($col = 0; $col < $highestColumnIndex; ++ $col) { 
     $cell = $worksheet->getCellByColumnAndRow($col, $row); 
     $val = $cell->getCalculatedValue(); 
     //$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val); 
     echo '<td>' . $val . '<br></td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
} 

for ($row = 11; $row <= $highestRow; ++ $row) { 
$val=array(); 
for ($col = 0; $col < $highestColumnIndex; ++ $col) { 
$cell = $worksheet->getCellByColumnAndRow($col, $row); 
$val[] = $cell->getValue(); 
} 
//Insert data from file to mysql 
$sql="INSERT INTO phpexcel(objekt_nr, objekt_rev, element_nr, element_langd, element_bredd,  element_hojd) 
VALUES ('".$val[1] . "','" . $val[2] . "','" . $val[3]. "','" . $val[4]. "','" . $val[5]. "','" .  $val[6]. "')"; 
//echo $sql."\n"; 
mysql_query($sql) or die('Invalid query: ' . mysql_error()); 
} 
?> 
+0

能分享從哪裏下載2個文件的鏈接Classes/PHPExcel .php和類/ PHPExcel/IOFactory.php – san 2016-11-17 06:47:05

+0

糾正N1CKN4M3的回答 每個for循環中'$ row'的值必須由'1'而不是'11'初始化,即它應該是:for($ row = 1 ; $ row <= $ highestRow; ++ $ row){ – OshoParth 2014-12-08 11:59:57