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
| Sub ChargementExcel()
'
'
'----------------------------------------------------------------------
'
'----------------------------------------------------------------------
'
' Déclarations et initialisations des variables locales.
'
Dim ListeFichierCSV() As String = Nothing
Dim CompteurFichier As Integer = 0
'
' Varaibles pour Excel
'
Dim xlapp As Excel.Application = Nothing
Dim Classeur As Excel.Workbook = Nothing
Dim Feuille As Excel.Worksheet = Nothing
'
'
'
MyPath = String.Format(Environment.CurrentDirectory)
ReDim Preserve ListeFichierCSV(1)
ListeFichierCSV(0) = "Données_IPC.csv"
ListeFichierCSV(1) = "Données_PrixMP.csv"
'
' Desctruction du classeur si celui-ci existe.
'
'Try
For i As Integer = ListeFichierCSV.GetLowerBound(0) To ListeFichierCSV.GetUpperBound(0)
Try
If My.Computer.FileSystem.FileExists(MyPath & "\" & ListeFichierCSV(i).Replace("csv", "xlsx")) Then
My.Computer.FileSystem.DeleteFile(MyPath & "\" & ListeFichierCSV(i).Replace("csv", "xlsx"))
End If
'
' Intitalisations des diverses variables Excel déclarées.
'
xlapp = New Excel.Application 'CreateObject("Excel.Application")
Classeur = xlapp.Workbooks.Add()
Feuille = Classeur.Worksheets.Add
'
' Propriétés de l'application xlapp
'
xlapp.Visible = True
xlapp.SheetsInNewWorkbook = 1
xlapp.ScreenUpdating = False
'
' Sauvegarde du classeur qui vient d'être créé.
'
Classeur.SaveAs(Filename:=MyPath & "\" & ListeFichierCSV(i).Replace("csv", "xlsx"), FileFormat:=XlFileFormat.xlWorkbookDefault, CreateBackup:=False)
'
'
'
With Feuille.QueryTables.Add(Connection:="TEXT;" & MyPath & "\" & ListeFichierCSV(i), Destination:=Feuille.Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = XlTextParsingType.xlDelimited
.TextFileTextQualifier = XlTextQualifier.xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
.TextFileTrailingMinusNumbers = True
.Refresh(BackgroundQuery:=False)
End With
Feuille.Name = Path.GetFileNameWithoutExtension(ListeFichierCSV(i))
'
'
'
For Each Feuil As Excel.Worksheet In Classeur.Sheets
If Feuil.Name.Contains("Feuil") Then
Console.WriteLine("Destruction de la feuille : " & Feuil.Name)
Feuil.Delete()
End If
Next
Catch ex As Exception
'
Console.WriteLine(ex.Message)
'
Finally
'
' Activation de la feuille contenant la tables des matières
'
Classeur.Worksheets(1).Activate()
'
' Instructions nécessaires pour une bonne sortie de MS Excel.
'
' Ces lignes ont été écrites en utilisant les informations
' disponibles sur le site:
'
' <a href="http://www.xtremevbtalk.com/showthread.php?p=1326018#post1326018" target="_blank">http://www.xtremevbtalk.com/showthre...18#post1326018</a>
'
' L'auteur du texte de la page ci-dessus est Mike Rosenblum, il a
' aussi son propre site:
'
' <a href="http://stackoverflow.com/users/10429/mike-rosenblum" target="_blank">http://stackoverflow.com/users/10429/mike-rosenblum</a>
'
GC.Collect()
GC.WaitForPendingFinalizers()
'GC.Collect()
'GC.WaitForPendingFinalizers()
'
'
'
'Marshal.FinalReleaseComObject(Feuille)
'
' Sauvegarde du classeur avant la fermeture de celui-ci.
'
Classeur.Close(SaveChanges:=True)
Marshal.FinalReleaseComObject(Classeur)
'
' FErmeture du serveur Excel.
'
xlapp.Quit()
Marshal.FinalReleaseComObject(xlapp)
End Try
Next i
End Sub |
Partager