
| auteur : SilkyRoad | Vba |
Option Explicit
Dim objDOM As DOMDocument
Sub Test ()
CreationFichierXML Worksheets (" Feuil1 " ). Range (" A1:F20 " )
End Sub
Sub CreationFichierXML (Plage As Range)
Dim XnodeRoot As IXMLDOMElement, oNode As IXMLDOMNode
Dim XNom As IXMLDOMElement
Dim Cmt As IXMLDOMComment
Dim Entete As Range, Cell As Range
Dim i As Integer, j As Integer
Set Entete = Plage. Rows (1 )
Set Plage = Plage. Offset (1 , 0 ). Resize (Plage. Rows . Count - 1 , Plage. Columns . Count )
Set objDOM = New DOMDocument
Set Cmt = objDOM. createComment (" Créé par " & Environ (" username " ) & " , le " & Date )
Set Cmt = objDOM. InsertBefore (Cmt, objDOM. ChildNodes . Item (0 ))
Set oNode = objDOM. createProcessingInstruction (" xml " , " version='1.0' encoding='ISO-8859-1' " )
Set oNode = objDOM. InsertBefore (oNode, objDOM. ChildNodes . Item (0 ))
Set XnodeRoot = objDOM. createElement (" MonTableau " )
objDOM. appendChild XnodeRoot
For j = 1 To Plage. Rows . Count
Set XNom = objDOM. createElement (" DonneeTableau " )
XNom. setAttribute Entete. Cells (1 , 1 ), Plage. Cells (j, 1 )
XnodeRoot. appendChild XNom
For i = 2 To Entete. Columns . Count
CreationElement Entete. Cells (1 , i), Plage. Cells (j, i), XNom
Next i
Next j
objDOM. Save " C:\Nom Fichier.xml "
Set XnodeRoot = Nothing
Set objDOM = Nothing
End Sub
Sub CreationElement (strElem As String , Donnee As Variant, oNom As IXMLDOMElement)
Dim XInfos As IXMLDOMNode
Set XInfos = objDOM. createElement (strElem)
XInfos. Text = Donnee
oNom. appendChild XInfos
End Sub
|
|
| auteur : SilkyRoad | Vba |
Sub ImporterFichierXML ()
Dim XM As XmlMap
ThisWorkbook. XmlImport _
URL:= " C:\Nom Fichier.xml " , _
ImportMap:= Nothing , _
Overwrite:= True , _
Destination:= Worksheets (" Feuil3 " ). Range (" $B$1 " )
Set XM = ThisWorkbook. XmlMaps (ThisWorkbook. XmlMaps . Count )
MsgBox " Import terminé " & vbCrLf & _
XM. RootElementName & vbCrLf & _
XM. Name & vbCrLf & _
XM. DataBinding . SourceUrl
End Sub
|
|
| auteur : SilkyRoad | Vba |
Sub SuppressionMappage ()
ThisWorkbook. XmlMaps (" MesDonnees_Mappage " ). Delete
End Sub
|
|
| auteur : SilkyRoad |
L'évènement Workbook_AfterXmlImport est déclenché après l'insertion ou l'actualisation des données xml
dans la feuille de calcul.
Le paramètre IsRefresh permet d'identifier si l'import provient d'une nouvelle source de données ou s'il
s'agit de l'actualisation d'un mappage existant dans la feuille. La valeur True est renvoyée s'il s'agit d'une actualisation.
Vba |
Private Sub Workbook_AfterXmlImport (ByVal Map As XmlMap, ByVal IsRefresh As Boolean, _
ByVal Result As XlXmlImportResult)
MsgBox IsRefresh & vbCrLf & _
Map. Name
End Sub
|
|
| auteur : SilkyRoad |
Placez un contrôle TreeView et un CommandButton dans un UserForm pour visualiser la structure du fichier xml.
Nécessite d'activer la référence Microsoft XML, vx.x.
Vba |
Option Explicit
Dim oDoc As MSXML2. DOMDocument
Private Sub CommandButton1_Click ()
Set oDoc = New DOMDocument
oDoc. async = False
oDoc. Load " C:\NomFichier.xml "
TreeView1. Nodes . Clear
AddNode oDoc. DocumentElement
End Sub
|
Vba |
Private Function AddNode (ByRef oElem As MSXML2. IXMLDOMNode , _
Optional ByRef oTreeNode As MSComctlLib. Node )
Dim oNewNode As MSComctlLib. Node
Dim oNodeList As MSXML2. IXMLDOMNodeList
Dim i As Long
If oTreeNode Is Nothing Then
Set oNewNode = TreeView1. Nodes . Add
oNewNode. Expanded = True
Else
Set oNewNode = TreeView1. Nodes . Add (oTreeNode, tvwChild)
oNewNode. Expanded = True
End If
Select Case oElem. NodeType
Case MSXML2. NODE_ELEMENT
oNewNode. Text = oElem. nodeName & " ( " & GetAttributes (oElem) & " ) "
Set oNewNode. Tag = oElem
Case MSXML2. NODE_TEXT
oNewNode. Text = " Text: " & oElem. NodeValue
Set oNewNode. Tag = oElem
Case MSXML2. NODE_CDATA_SECTION
oNewNode. Text = " CDATA: " & oElem. NodeValue
Set oNewNode. Tag = oElem
Case Else
oNewNode. Text = oElem. NodeType & " : " & oElem. nodeName
Set oNewNode. Tag = oElem
End Select
Set oNodeList = oElem. ChildNodes
For i = 0 To oNodeList. Length - 1
AddNode oNodeList. Item (i), oNewNode
Next i
End Function
|
Vba |
Private Function GetAttributes (ByRef oElm As MSXML2. IXMLDOMNode ) As String
Dim sAttr As String
Dim i As Long
sAttr = " "
For i = 0 To oElm. Attributes . Length - 1
sAttr = sAttr & oElm. Attributes . Item (i). nodeName & " =' " & _
oElm. Attributes . Item (i). NodeValue & " ' "
Next i
GetAttributes = sAttr
End Function
|
|
Consultez les autres F.A.Q's
Les sources présentés sur cette pages sont libre de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2008
Developpez LLC. Tout droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérets.
Cette page est déposée à la SACD.
|