tags:

views:

60

answers:

4

Can somebody send me PHP code which will read an excel 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

+2  A: 

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...

ircmaxell
+3  A: 

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

Charles
A: 

You can use a PEAR package Spreadsheet_Excel_Writer. link text

Alexander.Plutov
A: 

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

NAVEED