Convert an excel file to mysql table

Discussion in 'PHP' started by hendkandeel, Mar 9, 2009.

  1. #1
    Hi All,
    I need a way or script or a FREE tool to convert excel sheets to mysql tables
    I found tools on the nert but DEMO !!
    it converts first 5 rows only !!
     
    hendkandeel, Mar 9, 2009 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #2
    Why not use excel to save your file as a CSV (comma seperated values) and then use PHPmyadmin to import the data that way?
     
    Grit., Mar 9, 2009 IP
  3. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #3
    That would be the easiest way
    If not i did a quick google and found this

    Not sure if its any good but seems to do what you want, check it out
     
    m0nkeymafia, Mar 9, 2009 IP
  4. joxtechnology

    joxtechnology Peon

    Messages:
    146
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    save it first as csv file. then import it to your mysql database
     
    joxtechnology, Mar 9, 2009 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    Vooler, Mar 10, 2009 IP
  6. hendkandeel

    hendkandeel Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Dear ,Grit.,joxtechnology,
    I got the following error:
    Invalid field count in CSV input on line 1.
    ======================================
     
    hendkandeel, Mar 10, 2009 IP
  7. joxtechnology

    joxtechnology Peon

    Messages:
    146
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hmm. your mysql table should have same number fields. example if your csv/excel file contains 10 columns your mysql table should also have 10 fields to import the data.
     
    joxtechnology, Mar 11, 2009 IP
  8. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #8
    Well i suggest that please dont keep the column heading while saving as csv

    the second thing is if there are any fields which are strings and contains characters like , ' & etc then please enclose such columns in ""


    because csv file consist of comma separated values and any comma is considered to be a field separator so the there may be some comma in ur data which is reporting the php or mysql more number of columns than available
     
    bhagwant.banger, Mar 11, 2009 IP
  9. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #9
    Well if you have decided to work with CSV well and good, it will go pretty well. If you already have table structure well otherwise following function will create new table and create all fields text (although it i not good idea, but as requirement is not clear, I am assuming it)

    
    <?
    	$csv = "filename.csv";
    	$tab = "NewTableName";
    
    
    	mysql_connect("<DBHOST>","<DBUSER>","<DBPASSW>");
    	mysql_select_db("<DATABASE_NAME>");
    
    
    
    	$inserted = [B]trasnformCSV2MySQL[/B]($csv, $tab);
    	
    	echo "Inserted total <b>$inserted</b> rows to table $tab";
    
    
    
    	function trasnformCSV2MySQL($csv,$tab) {
    
    		$f = fopen($csv,"r");
    
    		$first_row = fgetcsv($f,2048); #assumed that first row or CSV is field names
    		$total_fields = count($first_row);
    		#create table
    
    		$all_fields = implode(" text,\r\n",$first_row)." text";
    		
    		mysql_query("CREATE TABLE `$tab` (
    		
    			$all_fields
    		
    		)") or die(mysql_error());
    		
    		$insertion_queries = Array();
    		
    		while($csv_row = fgetcsv($f,2048)) {
    			$fixed = array_slice($csv_row,0,$total_fields);
    			if(count($fixed)==1 and trim($fixed[0]) == "") #empty row
    				continue;
    			
    			foreach($fixed as $ke => $va) $fixed[$ke] = str_replace('"',"\\\"",trim($va));
    			$fixed = "\"".implode("\",\"",$fixed)."\"";
    			
    			$insertion_queries[]= "(".$fixed.")";
    			
    		}
    		$total_rows = count($insertion_queries);
    		$insertion_queries = implode(",\r\n",$insertion_queries);
    		mysql_query("INSERT INTO `$tab`
    				VALUES
    				$insertion_queries") or die(mysql_error());
    				
    		return $total_rows;
    	
    	}
    
    
    ?>
    
    PHP:


    function trasnformCSV2MySQL takes 2 arguments first is csv file path, second is table name. It is your responsibility to delete all enteries of table first if you want it to be empty. Also, it creates table and does not check if alreay exists, so drop the testing table first, if same name exists.

    function requires little modifications to make it work with existing table btw.


    I hope it helps.

    regards
     
    Vooler, Mar 12, 2009 IP
  10. hendkandeel

    hendkandeel Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I invented a strange way !!
    I imported excel into Access
    and found a free tool to convert Access db to Mysql !!

    Nice?
     
    hendkandeel, Mar 18, 2009 IP
  11. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #11
    Well, basially purpose of such script is to perform everything at server-side.

    Here is one more similar script that was also coed by me.
     
    Vooler, Mar 18, 2009 IP
  12. internetstromer

    internetstromer Member

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #12
    I purchased one software called exceltomysql which is good it convert my excel sheet into the table and column first row into the field name and it converts rest of the raw as number of entries..!!!

    Cool one
     
    internetstromer, Mar 18, 2009 IP
  13. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #13
    The one I am pointing to is too flexible, and allows you to import to selective fields of a db table as well as skip any columns from CSV input. + it supports 3 SQL servers not just mysql.

    regards
     
    Vooler, Mar 18, 2009 IP
  14. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hello,

    Yup, You can definately convert excel file into mysql table..We already did this..We will help you if you need help for excel sheet into mysql table


    Please send us PM for this


    Regards
    Stylesofts Developing Team
     
    Stylesofts, Mar 19, 2009 IP
  15. phpdev001

    phpdev001 Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #15
    do with following step excel to mysql convert.

    1. convert excel file to msaccess. you just need to open excel file in access.

    2. then convert mdb to mysql by MySQL GUI Tools from migration option. MySQL GUI Tools is free, you can download from mysql site.

    hope it will be help you
     
    phpdev001, Mar 19, 2009 IP