Import Excel data in to mysql with PHP
Can somebody send me PHP code which will read an exc开发者_如何学Goel file and write the data of that excel file into database. It should also read multiple tabs of that excel file and perform the same operation.
Thanks
We will not send you the codes.
We can, instead, point you at helpful libraries that you can use. Check out PHPExcel, it can work with XSLX files. There's also php-excel, which claims to be more lightweight than PHPExcel.
Incidentally, these were the top results when searching Google for php excel
Personally, I prefer using ODBC
, which lets you treat the excel file as a database and query against it...
$file = realpath($file);
$dir = dirname($file);
$dsn = 'Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq='.$file.';';
$dsn .= 'DefaultDir='.$dir.';';
$connection = odbc_connect($dsn, '', '');
if ($connection === false) {
die('Could not connect to Excel File');
}
$sql = 'SELECT * FROM [Sheet1]';
$result = odbc_exec($connection, $sql);
if ($result === false) {
die('Query Error: ['.odbc_error($connection).'] '.
odbc_errormsg($connection));
}
$rows = array();
while ($row = odbc_fetch_array($result)) {
$rows[] = $row;
}
The cool part of this, is that Excel automatically pulls the column name from the first row of the file...
You can use a PEAR package Spreadsheet_Excel_Writer. link text
Look at this:
How to get data from Excel:
http://www.eephp.com/
http://www.ustrem.org/en/articles/reading-xls-with-php-en/
How to store data into database if you are using mysql:
http://www.w3schools.com/PHP/php_mysql_insert.asp
精彩评论