1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

excel to mysql import script

Sometimes a client requires you to import some data from an excel sheet to mysql. You can do it by hand if there are only a few records. However, if there are a few hundred records, you need some kind of script to do this. I had this same problem. So i wrote something in autoit and thought i might share it. The script itself it very simple and very easy to modify.
You need to download and install autoit before you can run this. I could just give you the executable, but no one would download it and no one should. Besides the script is very specific and is meant to be modified to suit your need.

here the file in case wordpress butchers my script.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <Excel.au3>
#include <Array.au3>
#Include <Date.au3>
;file picker
$excelFile = FileOpenDialog("Select an excel file",@DesktopDir,"Excel (*.xls)" ,1+2)
If Not @error Then
;create main window
$mainGui = GUICreate("Importing",350,150)
GUISetState(@SW_SHOW)
$status = GUICtrlCreateEdit("Begin importing",10,10,330,130,$ES_AUTOVSCROLL+$ES_AUTOHSCROLL+$ES_MULTILINE+$ES_READONLY)
addStatus("File selected:"&amp;$excelFile)
doExport($excelFile)
Else
MsgBox(0,"Error","Error opening file")
Exit
EndIf
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
Func doExport($file)
$fpExcel = _ExcelBookOpen($file,0,1)
If @error = 1 Then
addStatus("Unable to Create the Excel Object")
Exit
ElseIf @error = 2 Then
addStatus("File does not exist - Shame on you!")
Exit
Else
addStatus("File open successfully")
EndIf
$done = False
$i = 2
While Not $done
;read a row from excel file
$excelArray = _ExcelReadArray($fpExcel,$i,1,9,0,0)
If $excelArray[0] <> "" Then
addStatus("reading Row " & $i)
;insert into sql file
arrayToSql($excelArray)
Else
addStatus("Finsihed reading excel file")
addStatus($i-2 & " rows converted into sql statement")
$done = True
EndIf
$i += 1
WEnd
_ExcelBookClose($fpExcel)
EndFunc
Func arrayToSql($array)
;save sql file to desktop
$sqlFile = FileOpen(@DesktopDir&"/ExcelToSql-"&@MDAY&"-"&@MON&"-"&@YEAR&".sql",1+8)
$sqlStatement = "INSERT INTO `_products` (`int_id`, `product_type`, `title`, `description`, `synopsis`, `publication_date`, `price`, `categories`, `list`, `type_id`, `processed`, `write_err`) VALUES ("
$sqlStatement &= "'"& escapeString($array[0]) & "'," ;int_id
$sqlStatement &amp;= "'"& escapeString($array[1]) & "'," ;product_type
$sqlStatement &amp;= "'"& escapeString($array[2]) & "'," ;title
$sqlStatement &amp;= "'"& escapeString($array[3]) & "'," ;description
$sqlStatement &amp;= "'"& escapeString($array[4]) & "'," ;synopsis
$sqlStatement &amp;= "'"& toExcelDate($array[5]) & "',"  ;date
$sqlStatement &amp;= "'"& escapeString($array[6]) & "'," ;price
$sqlStatement &amp;= "'"& escapeString($array[7]) & "'," ;category
$sqlStatement &amp;= "'"& escapeString($array[8]) & "'," ;list
$sqlStatement &amp;= "'','N','N'"
$sqlStatement &amp;= ");"
FileWriteLine($sqlFile,$sqlStatement)
FileClose($sqlFile)
EndFunc
Func escapeString($string)
;replace anything that is not a number, letter or common punctuation
$string = StringRegExpReplace($string,"[^0-9a-zA-Z.,\\/\!@#$%\^\&amp;\*\(\)\-_\+=\h<>]","")
;escape single quote
$string = StringRegExpReplace($string,"[']","\'")
Return $string
EndFunc
Func toExcelDate($dateString)
$yearString = StringMid($dateString,1,4)
$monthString = StringMid($dateString,5,2)
$dayString = StringMid($dateString,7,2)

return $dayString&amp;"-"&amp;StringMid(_DateToMonth($monthString,1),1,3)&amp;"-"&amp;$yearString
EndFunc
Func addStatus($text)
GUICtrlSetData($status,@CRLF&amp;$text,1)
EndFunc
Share:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Twitter
  • Yahoo! Bookmarks
  • #1
    Posted by Pett on July 18th, 2009 at 5:59 AM

    Can i take a one small picture from your blog?

  • #2
    Posted by Funky Dude on July 18th, 2009 at 10:36 AM

    yes and why?

  • #3
    Posted by SonyaSunny on July 24th, 2009 at 3:09 PM

    Thanks for article. Everytime like to read you.
    SonyaSunny

  • #4
    Posted by Robor on July 28th, 2009 at 12:19 PM

    Everything dynamic and very positively! :)
    Robor

Share your opinion! Post your thoughts.