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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#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:"&$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 &= "'"& escapeString($array[1]) & "'," ;product_type
$sqlStatement &= "'"& escapeString($array[2]) & "'," ;title
$sqlStatement &= "'"& escapeString($array[3]) & "'," ;description
$sqlStatement &= "'"& escapeString($array[4]) & "'," ;synopsis
$sqlStatement &= "'"& toExcelDate($array[5]) & "'," ;date
$sqlStatement &= "'"& escapeString($array[6]) & "'," ;price
$sqlStatement &= "'"& escapeString($array[7]) & "'," ;category
$sqlStatement &= "'"& escapeString($array[8]) & "'," ;list
$sqlStatement &= "'','N','N'"
$sqlStatement &= ");"
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.,\\/\!@#$%\^\&\*\(\)\-_\+=\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&"-"&StringMid(_DateToMonth($monthString,1),1,3)&"-"&$yearString
EndFunc
Func addStatus($text)
GUICtrlSetData($status,@CRLF&$text,1)
EndFunc

4 Comments
Can i take a one small picture from your blog?
yes and why?
Thanks for article. Everytime like to read you.
SonyaSunny
Everything dynamic and very positively!
Robor