Export EXCEL to MYSQL table without saving xls as .csv

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

Export EXCEL to MYSQL table without saving xls as .csv

Post 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
ody3307
Forum Newbie
Posts: 21
Joined: Wed Jul 30, 2008 7:29 am

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

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