Il y a quelques années j'avais écrit un code pour convertir Excel ou un CSV en base de données. Il mériterait d'être améliorer mais il fonctionne.
La première ligne du tableau doit contenir les titres des colonnes. Parfois le code de la partie Excel beugue, il suffit de relancer le code.
Code : Tout sélectionner
#include <Array.au3>
#include <SQLite.au3>
HotKeySet('!x', 'MsgError');alt x
Global $saveDB = FileSaveDialog("Sauvegarde de la base",@DesktopDir,"Base(*.db)",0,"BaseSQLite_1.db")
If @error Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
If FileExists($saveDB) Then
$rep = MsgBox(4,"LA BASE EXISTE","Voulez vous détruire la base ?")
If $rep = 6 Then FileRecycle($saveDB)
EndIf
Global $table = InputBox("NOM DE LA TABLE","Entrez le nom de la table","MyTable")
If @error Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
If $table = "" Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
Global $path = FileOpenDialog("CLASSEUR EXCEL",@DesktopDir,"Excel(*.xls;*.xlsx;*.xlsm;*.xlsb;*.csv)",0,"clubs_Essai.csv")
If @error Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
Global $lire = InputBox("CHOIX DE LA FEUILLE EXCEL","Entrez le N° de la feuille",1)
$lire = Int($lire)
If @error Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
If $lire = "" Then Exit MsgBox(0,"ERREUR",'Pas de choix Exit',2)
$temps = TimerInit()
Local $oExcel = ObjCreate("Excel.Application")
If @error Then Exit MsgBox(16,"Error","Echec Objet Excel")
ToolTip("Lecture d'Excel",100,110)
If Not FileExists($path) Then Exit MsgBox(16,"Error","Classeur inconnu")
$oWorkbook = $oExcel.WorkBooks.Open($path, Default, Default, Default, Default, Default)
$oExcel.Visible = 0
If $oExcel.Sheets.Count < $lire Then ExcelError("Feuille demandée inexistante")
$oExcel.Sheets($lire).Select
Local $arrayxls = $oExcel.ActiveSheet.UsedRange
Local $array = $arrayxls.Value
_ArrayTranspose($array)
$arrayT = $array
$oExcel.ActiveWorkbook.Saved = True
$oExcel.WorkBooks.close
$oExcel = 0
If Not IsArray($arrayT) Then Exit MsgBox(16,"Error","Echec recherche Excel")
$nbLignes = UBound($arrayT,1)
$nbColonnes = UBound($arrayT,2)
;_ArrayDisplay($arrayT)
Local $_FileExists = FileExists($saveDB)
_SQLite_Startup()
If @error Then
MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded!")
Exit -1
EndIf
_SQLite_Open($saveDB)
If @error Then MsgError()
Local $aResult, $iRows, $iColumns
If $_FileExists = 1 Then;si base existe
_SQLite_GetTable2d(-1, "SELECT type,name FROM sqlite_master;", $aResult, $iRows, $iColumns)
;_ArrayDisplay($aResult)
For $i = 1 To UBound($aResult) - 1
If $aResult[$i][0] = "table" Then;si table
If $aResult[$i][1] = $table Then;si même nom de table
_SQLite_GetTable2d(-1, "PRAGMA table_info( "& $table &" );", $aResult, $iRows, $iColumns)
If $nbColonnes = 0 Then
$nbColonnesA = 1;1D
Else
$nbColonnesA = $nbColonnes;2D
EndIf
If $nbColonnesA <> $iRows Then MsgError("nb colonnes différent");si nb colonnes différents
ExitLoop
EndIf
EndIf
Next
EndIf
Local $coll = "" ;noms des colonnes
If $nbColonnes = 0 Then ;array 1d
$coll = _SQLite_Escape($arrayT[0])
Else
For $i = 0 To $nbColonnes - 1 ;array 2d
If $i <> $nbColonnes - 1 Then
$virg = ", "
Else
$virg = ""
EndIf
;modifie nom de colonne en double
$val = $arrayT[0][$i]
$valN = $arrayT[1][$i]
If StringInStr($coll,$val) > 0 Or $val = "" Then $val = $val & $i
Local $format = " text"
If IsString($valN) Then $format = " TEXT"
If IsInt($valN) Then $format = " INTEGER"
If IsFloat($valN) Then $format = " REAL"
If StringLen($valN) = 14 And StringMid($valN, 9,6) = 000000 And StringMid($valN, 1,4) <= @YEAR + 1 And StringMid($valN, 5,2) < 13 And StringMid($valN, 7,2) <= 31 Then $format = " DATE"
$coll &= _SQLite_Escape($val) & $format & $virg
;ConsoleWrite($valN &">>"& $format & @CRLF)
Next
EndIf
If $SQLITE_OK <> _SQlite_Exec(-1, "CREATE TABLE IF NOT EXISTS "& $table &" ("& $coll &")") Then MsgError()
_SQLite_Exec(-1, 'BEGIN')
If $nbColonnes = 0 Then ;Array 1D
For $i = 1 To $nbLignes - 1
$sql = 'INSERT INTO '& $table &' VALUES ('
$sql &= _SQLite_Escape($arrayT[$i]) & ")"
If $SQLITE_OK <> _SQLite_Exec(-1, $sql) Then MsgError()
ToolTip( $i &" /"& $nbLignes - 1,100,110)
$sql = ""
Next
Else ;Array 2D
For $i = 1 To $nbLignes - 1
$sql = 'INSERT INTO '& $table &' VALUES ('
For $y = 0 To $nbColonnes - 1
$valeur = $arrayT[$i][$y]
_SQLite_GetTable2d(-1, "PRAGMA table_info( "& $table &" );", $aResult, $iRows, $iColumns)
ConsoleWrite($aResult[$y + 1][2] & @CRLF)
If $aResult[$y + 1][2] = "DATE" Then
$split1 = StringMid($valeur, 7,2)
$split2 = StringMid($valeur, 5,2)
$split3 = StringMid($valeur, 1,4)
$valeur = StringFormat("%04i-%02i-%02i" ,$split3,$split2,$split1)
$sql &= "DATE(" & _SQLite_Escape($valeur) & "), "
Else
$sql &= _SQLite_Escape($valeur) & ", "
EndIf
Next
$sql = StringTrimRight($sql, 2) & ");"
If $SQLITE_OK <> _SQLite_Exec(-1, $sql) Then MsgError()
ToolTip( $i &" /"& $nbLignes - 1,100,110)
$sql = ""
Next
EndIf
_SQLite_Exec(-1, 'END')
_SQLite_Close()
_SQLite_Shutdown()
$tempsX = (TimerDiff($temps)/1000)/60
ConsoleWrite("temps écoulé en mn " & $tempsX & @CR)
$ttp = (TimerDiff($temps))/$nbLignes - 1
ConsoleWrite("temps écriture/ligne " & $ttp & @CR)
ToolTip(">>>>>>>>> Réussite",50,150)
Sleep(2000)
ToolTip("")
Func ExcelError($msgXls)
$oExcel.ActiveWorkbook.Saved = True
$oExcel.WorkBooks.close
$oExcel = 0
Exit MsgBox(16,"Error",$msgXls)
EndFunc
Func MsgError($msg = "")
If $msg = "" Then $msg = "Error Code: " & _SQLite_ErrCode() & @CRLF & "Error Message: " & _SQLite_ErrMsg()
MsgBox(16, "SQLite Error", $msg)
_SQLite_Close()
_SQLite_Shutdown()
ToolTip("")
Exit
EndFunc
Si la base existe, il est proposé de la détruire. Si vous dites non et que vous nommez la table du même nom de l'existante, les données seront rajoutées à la suite sauf si le nombre de colonnes n'est pas identique.