我想使用php將excel中的數據導入到mySQL DB中。我嘗試過使用其他問題中解釋的方式,但沒有爲我解決。請讓我知道如何使用php將數據導入到數據庫。如何使用php將excel文件導入到mySQL數據庫
另外,請讓我知道在哪裏放置Excel文件上傳,我的意思是在系統中的位置。
我想使用php將excel中的數據導入到mySQL DB中。我嘗試過使用其他問題中解釋的方式,但沒有爲我解決。請讓我知道如何使用php將數據導入到數據庫。如何使用php將excel文件導入到mySQL數據庫
另外,請讓我知道在哪裏放置Excel文件上傳,我的意思是在系統中的位置。
方法1.you可以使用負載數據命令
http://blog.tjitjing.com/index.php/2008/02/import-excel-data-into-mysql-in-5-easy.html
方法2的Excel讀者
https://code.google.com/p/php-excel-reader/
方法3. parseCSV
https://github.com/parsecsv/parsecsv-for-php
方法4。 (PHP 4,PHP 5)fgetcsv
http://in1.php.net/fgetcsv
哪裏可以在我的系統中放置excel閱讀器的代碼? – 2015-02-11 06:52:28
請參閱此PHP代碼
<?php
//table Name
$tableName = "MyTable";
//database name
$dbName = "MyDatabase";
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
//get the first row fields
$fields = "";
$fieldsInsert = "";
if (($handle = fopen("test.csv", "r")) !== FALSE) {
if(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$fieldsInsert .= '(';
for ($c=0; $c < $num; $c++) {
$fieldsInsert .=($c==0) ? '' : ', ';
$fieldsInsert .="`".$data[$c]."`";
$fields .="`".$data[$c]."` varchar(500) DEFAULT NULL,";
}
$fieldsInsert .= ')';
}
//drop table if exist
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$tableName."'"))>=1) {
mysql_query('DROP TABLE IF EXISTS `'.$tableName.'`') or die(mysql_error());
}
//create table
$sql = "CREATE TABLE `".$tableName."` (
`".$tableName."Id` int(100) unsigned NOT NULL AUTO_INCREMENT,
".$fields."
PRIMARY KEY (`".$tableName."Id`)
) ";
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not create table: ' . mysql_error());
}
else {
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$fieldsInsertvalues="";
//get field values of each row
for ($c=0; $c < $num; $c++) {
$fieldsInsertvalues .=($c==0) ? '(' : ', ';
$fieldsInsertvalues .="'".$data[$c]."'";
}
$fieldsInsertvalues .= ')';
//insert the values to table
$sql = "INSERT INTO ".$tableName." ".$fieldsInsert." VALUES ".$fieldsInsertvalues;
mysql_query($sql,$conn);
}
echo 'Table Created';
}
fclose($handle);
}
?>
我得到的錯誤如下fopen(protected/extensions/phpexcel/test1.xls):無法打開流:沒有這樣的文件或目錄 – 2015-02-11 07:24:41
更新文件的目錄只是一個文件名,並確保您有權限用於訪問文件 – user3925225 2015-02-11 08:42:35
我試過只給出文件名。請讓我知道我必須放置文件的位置。現在我已經將它放在C:\ xampp \ htdocs \ www \ ClaimSystem \ claimNew \ protected \ extensions \ phpexcel – 2015-02-11 09:09:27
這是不是很難?你可以寫你自己的實行。嘗試不使用一些第三方軟件 – 2015-02-11 06:21:34
我是新來的PHP。你能幫我寫代碼嗎? – 2015-02-11 06:24:05