Page 1 of 1

Export EXCEL to MYSQL table without saving xls as .csv

Posted: Mon Jul 28, 2008 1:23 pm
by pavanpuligandla
Hii,,
i'm trying to export an excel workbook to mysql table but couldnt succeed, at last i found a solution which is not that much perfect to implement( i,e import CSV to Mysql table.)
I want to export any xls file to the specified database table, but in my code i'm predefining the .csv file which is to be exported to the mysql table..
kindly go thru this and do the need ful.. the code is as follows :;

Code: Select all

<?PHP
 
//Connect to mysql server
$link=mysql_connect("localhost","root","");
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db=mysql_select_db("college");
if(!$db) {
die("Unable to select database");
}
 
$fcontents = file ('http://localhost/Project/spsheet.xls');
# expects the csv file to be in the same dir as this script
 
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
echo "$line<BR>";
$arr = explode("','", $line);
echo "$arr";
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
$sql = "insert into subject values (". implode("'", $arr).")";
#$sql = "insert into test values ('". $arr ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>
my motto is to implement the code which it should read the contents of input <b>ANY</b> .xls file instead of predefining the path to only a selected single .xls file...i dun wanna save my xls file as .csv and then exporting to database, ineed to export the xls file itself to database.

[$fcontents = file ('http://localhost/Project/spsheet.xls');]-- predefined path of xls file..

Many thanks
pavan.p

Re: Export EXCEL to MYSQL table without saving xls as .csv

Posted: Thu Jul 31, 2008 6:51 pm
by ody3307
The problem is, xls files are not plain text. csv files, on the other hand are plain text. I can think of no easy way to do this except csv.