The_chosen_one a écrit :
Bonjour,
Comme je trouvais pas sur le net, j'ai fait un petit script d'importation d'un ldif vers excel.
Je suis pas un pro du format ldif donc il se peut que j'ai pris quelques libertés dans la retranscription.
Commentaires constructifs bienvenus, mais pour l'instant ça à l'air de tourner correctement.
Code :
- Option Explicit
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' ImportTextFile
- ' This imports a text file into Excel.
- ' From 1. Option Explicit
- 2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- 3. ' ImportTextFile
- 4. ' This imports a text file into Excel.
- 5. ' From http://www.cpearson.com/excel/ImpText.aspx
- 6. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- 7. Public Sub ImportTextFile(FName As String)
- 8.
- 9. Dim RowNdx As Long
- 10. Dim WholeLine As String
- 11. Dim Pos As Integer
- 12.
- 13. Dim entete(255) As String
- 14. Dim taille As Integer
- 15. taille = 0
- 16.
- 17. Dim i As Integer
- 18. Dim trouve As Integer
- 19. Dim a, b As String
- 20.
- 21. RowNdx = 2
- 22.
- 23. Application.ScreenUpdating = False
- 24. On Error GoTo EndMacro:
- 25.
- 26. Open FName For Input Access Read As #1
- 27.
- 28. While Not EOF(1)
- 29. Line Input #1, WholeLine
- 30. While Not WholeLine = ""
- 31. Line Input #1, WholeLine
- 32. If Left(WholeLine, 1) = " " Then
- 33. Cells(RowNdx, trouve).Value = Cells(RowNdx, trouve).Value & Right(WholeLine, Len(WholeLine) - 1)
- 34. Else
- 35. Pos = InStr(WholeLine, ":: " )
- 36. If Pos = 0 Then Pos = InStr(WholeLine, ": " )
- 37. If Not Pos = 0 Then
- 38. a = Left(WholeLine, Pos - 1)
- 39. b = Right(WholeLine, Len(WholeLine) - (Pos + 1))
- 40. trouve = 0
- 41. For i = 1 To taille
- 42. If entete(i) = a Then trouve = i
- 43. Next
- 44. If trouve = 0 Then
- 45. entete(taille + 1) = a
- 46. trouve = taille + 1
- 47. taille = taille + 1
- 48. Cells(RowNdx, trouve).Value = b
- 49. Cells(1, trouve).Value = a
- 50. Else
- 51. If Cells(RowNdx, trouve).Value = "" Then
- 52. Cells(RowNdx, trouve).Value = b
- 53. Else
- 54. Cells(RowNdx, trouve).Value = Cells(RowNdx, trouve).Value & "|" & b
- 55. End If
- 56. End If
- 57. End If
- 58. End If
- 59. Wend
- 60. RowNdx = RowNdx + 1
- 61. Wend
- 62.
- 63. EndMacro:
- 64. On Error GoTo 0
- 65. Application.ScreenUpdating = True
- 66. Close #1
- 67. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- 68. ' END ImportTextFile
- 69. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- 70. End Sub
- 71.
- 72. Sub DoTheImport()
- 73. ImportTextFile FName:="c:\ldif.txt"
- 74. End Sub
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Public Sub ImportTextFile(FName As String)
- Dim RowNdx As Long
- Dim WholeLine As String
- Dim Pos As Integer
- Dim entete(255) As String
- Dim taille As Integer
- taille = 0
- Dim i As Integer
- Dim trouve As Integer
- Dim a, b As String
- RowNdx = 2
- Application.ScreenUpdating = False
- On Error GoTo EndMacro:
- Open FName For Input Access Read As #1
- While Not EOF(1)
- Line Input #1, WholeLine
- While Not WholeLine = ""
- Line Input #1, WholeLine
- If Left(WholeLine, 1) = " " Then
- Cells(RowNdx, trouve).Value = Cells(RowNdx, trouve).Value & Right(WholeLine, Len(WholeLine) - 1)
- Else
- Pos = InStr(WholeLine, ":: " )
- If Pos = 0 Then Pos = InStr(WholeLine, ": " )
- If Not Pos = 0 Then
- a = Left(WholeLine, Pos - 1)
- b = Right(WholeLine, Len(WholeLine) - (Pos + 1))
- trouve = 0
- For i = 1 To taille
- If entete(i) = a Then trouve = i
- Next
- If trouve = 0 Then
- entete(taille + 1) = a
- trouve = taille + 1
- taille = taille + 1
- Cells(RowNdx, trouve).Value = b
- Cells(1, trouve).Value = a
- Else
- If Cells(RowNdx, trouve).Value = "" Then
- Cells(RowNdx, trouve).Value = b
- Else
- Cells(RowNdx, trouve).Value = Cells(RowNdx, trouve).Value & "|" & b
- End If
- End If
- End If
- End If
- Wend
- RowNdx = RowNdx + 1
- Wend
- EndMacro:
- On Error GoTo 0
- Application.ScreenUpdating = True
- Close #1
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' END ImportTextFile
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- End Sub
- Sub DoTheImport()
- ImportTextFile FName:="c:\ldif.txt"
- End Sub
|
|