Jump to content

[SOLVED] Importing Data From Microsoft Excel into MySQL database


PC Nerd

Recommended Posts

Hi,

 

Im looking for a method if importing Excel Data into my MySQL database.  I have a feeling that the only way will to have it exported as a CSV file - and then have PHP transfer it ( INSERT INTO) - my database - but Im looking for an easier option.

 

If anyone can suggestion an easy way to do this I would be most grateful. I would cop the data across manually - excelt for that fact that Its over 800 records - so its a little un managable.

 

The data comes from another database, however I dont have access to it ( to export as SQL in MySQL compatability mode - however Im looking at options) - so I only have a CSV file that comes out of that database.  I can ensure that field names are the same as my MySQL database - all that isnt an issue - its just transfering the raw data across that is the issue.

 

Once again - any suggestions would be great.

 

Thanks in advance

Link to comment
Share on other sites

someone else had a similar question i gave them this code

<pre>
<?php
$con=mysql_connect();
mysql_select_db('test');
//number of lines to skip
$skip=1;
$current_line=1;


$sheet='test.txt';
$file=fopen($sheet,'r');
while(!feof($file)){
$line=fgets($file);
//add this
if($current_line>$skip){


	$line=explode("\t",$line);
	$query='INSERT INTO `table` VALUES (';
	foreach($line as $key => &$value){
		if($key==0){
			$query.="'".$value."'";
		}else{
			$query.=", '".$value."'";
		}
	}
	$query.=');';
	echo $query."\n";
	mysql_query($query,$con);
//add this
}
$current_line++;


}
fclose($file);


?>
</pre> 

 

this is designed for xls files saved as txt but if you want change the "\t" to "," on line 16 to use csv

and set the number skip according to how many lines are taken up by titles and headers

 

Scott.

Link to comment
Share on other sites

So far I've found:

 

http://mysql-migration-toolkit.intelligent-converters.qarchive.org/  - $99

 

If you have a way to convert xls to dbf, you could check out http://www.downloadatoz.com/compare/dbf-converter.html

One of those will convert dbf into sql queries.

 

I just found one:

 

http://xls-excel-to-dbf.whitetown-software.alienpicks.com/

 

So you could use the 3rd link to go from xls to dbf, then the second link to go dbf to sql queries. 

 

Not much there, but it might help.

Link to comment
Share on other sites

Some options

 

1 ) save as csv then bulk load using MySql LOAD DATA INFILE

 

2 ) save as csv and use a script to read and load

 

3 ) use COM to read the excel file directly

 

4 ) set up an excel odbc connection and use a script to read and load

 

5 ) StarTrek method - "Computer - transfer file from Excel to MySQL"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.