Excel sheet to sql server database

Status
Not open for further replies.

maitrasagnik

Right off the assembly line
Can anyone tell me how to transfer and pick data from an excel sheet and put that in the table of a database in SQL?
 
Last edited by a moderator:

victor_rambo

हॉर्न ओके प्लीज़
1. Convert the spreadsheet into CSV or tab delimited file using the "Save as" option.
2. Use the file() function to get every line in an array.
3. Explode the string at tab(/t) or comma(,).
4. Prepare the MySQL inset into query and execute.

Example usage:
The excel sheet saved as CSV file xls2mysql.php:
Code:
Amitabh Bachhan, 62, Male
Emma Watson, 18, Female
Daniel Radcliffe,18,Male

The PHP code:
PHP:
<?php
$xls=file("xls2mysql.csv");
foreach($xls as $row)
{
$fields=explode(",",$row);//comma if csv; /t(tab) if tab demilted.
echo "$fields[0] ";//instead of echoing the fileds, you should prepare the mysql insert query
echo "$fields[1] ";
echo "$fields[2] ";
echo "<br>";
}
?>

PS: I have written this code fro MySQL, but it WON'T differ for MS SQL
 
Last edited:
Status
Not open for further replies.
Top Bottom