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
| Option Explicit
Sub testx()
Dim doc As Object, plage, xxml, IE
Set plage = Range("a1:i54")'change ta plage ici c'est tout!!!!!
Set doc = CreateObject("htmlfile")
basetable doc, plage, xxml 'creation de la table de base et recuperation du xml correspondant
doc.body.innerhtml = htmltextecell(xxml, doc) 'ajout du innerhtml et changement des id avec les données du xml
' apercu instantané dans internet explorer
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.navigate "about:blank"
'IE.document.write html_with_CSStyle2(doc, PLAGE, xxml)
IE.document.write html_with_CSStyle2(doc, plage, xxml)
End Sub
Function html_with_CSStyle2(doc, plage, xxml)
Dim docxml As New MSXML2.DOMDocument, Noeuds As MSXML2.IXMLDOMNodeList, SubNoeuds As MSXML2.IXMLDOMNodeList
Dim balise As IXMLDOMNode, subBalise As IXMLDOMNode, Element As IXMLDOMElement, ElementST As IXMLDOMElement, noeud As IXMLDOMElement
Dim i#, A#, F#, ppx#, b$, FZ$, ids, TD, TDS, tdi, styles, Attributs, Fonts, StyleB$, Bweight$, BdColor$
FZ = val((ThisWorkbook.styles("Normal").font.Size) * ppx) & "px"
xxml = Replace(Replace(Replace(plage.Value(xlRangeValueXMLSpreadsheet), "ss:Data", "Data"), "<Borders>", ""), "</Borders>", "")
If Not docxml.LoadXML(xxml) Then err.Raise docxml.parseError.ErrorCode, , docxml.parseError.reason
With CreateObject("WScript.Shell"): ppx = .RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI") / 72: End With
Set TDS = doc.getElementsByTagName("TD")
With docxml
Set Noeuds = docxml.getElementsByTagName("Style")
For Each balise In Noeuds
'Debug.Print Balise.XML
Set ElementST = balise
ids = ElementST.getAttribute("ss:ID")
For tdi = 0 To TDS.Length - 1
If TDS(tdi).iD = ids Then
Set TD = TDS(tdi)
If IsDate(TD.innertext) Or IsNumeric(TD.innerhtml) Then TD.Style.textalign = "right"
For Each subBalise In balise.ChildNodes
Set Element = subBalise
Set Attributs = Element.Attributes
If Attributs.Length <> 0 Then
For A = 0 To Attributs.Length - 1
Select Case Attributs(A).BaseName
Case "FontName": TD.Style.fontfamily = Attributs(A).Value
Case "Size": TD.Style.FontSize = Round(val(Attributs(A).Value)) * ppx
Case "Bold": TD.innerhtml = "<B>" & TD.innerhtml & "</B>"
Case "Italic": TD.innerhtml = "<i>" & TD.innerhtml & "</i>"
Case "StrikeThrough": TD.innerhtml = "<s>" & TD.innerhtml & "</s>"
Case "Underline": TD.innerhtml = "<u>" & TD.innerhtml & "</u>"
Case "Color"
If Element.tagName = "Font" And Not TD.innerhtml Like "*FONT*" Then TD.Style.Color = Attributs(A).Value
If Element.tagName = "Interior" Then TD.Style.backgroundcolor = Attributs(A).Value
Case "Horizontal": TD.Style.textalign = Attributs(A).Value
Case "Vertical": TD.Style.verticalalign = Replace(Attributs(A).Value, "Center", "Middle")
Case "VerticalAlign":
If Attributs(A).Value = "Superscript" Then TD.innerhtml = "<sup>" & TD.innerhtml & "</sup>"
If Attributs(A).Value = "Subscript" Then TD.innerhtml = "<sub>" & TD.innerhtml & "</sub>"
Case "Position":
Debug.Print Element.getAttribute("ss:LineStyle")
StyleB = Replace(Replace(Replace(Replace(Element.getAttribute("ss:LineStyle"), "Continuous", "solid "), "SlantDashDot", "dashed "), "Dash", "dashed "), "Dot", "dotted ")
Bweight = Element.getAttribute("ss:Weight") & "px "
If StyleB = "dotted " Then Bweight = "2px "
If Element.getAttribute("ss:LineStyle") = "DashDot" Then StyleB = "dashed ": Bweight = "3px "
If IsNull(Element.getAttribute("ss:Color")) Then BdColor = "#000000" Else BdColor = Element.getAttribute("ss:Color")
Select Case Attributs(A).Value
Case "Top": TD.Style.bordertop = Bweight & StyleB & " " & BdColor
Case "Left": TD.Style.borderleft = Bweight & StyleB & " " & BdColor
Case "Right": TD.Style.borderright = Bweight & StyleB & " " & BdColor
'Case "Bottom": TD.Style.borderbottom = Bweight & StyleB & " " & BdColor
End Select
End Select
Next
End If
Next
TD.innerhtml = "<FONT>" & TD.innerhtml & " </FONT>"
If TD.Children.Length > 0 Then TD.Children(0).Style.MarginLeft = "2px": TD.Children(0).Style.MarginRight = "1px"
End If
Next
Next
End With
html_with_CSStyle2 = doc.body.innerhtml
End Function
Function htmltextecell(xxml, doc)
Dim docxml As New MSXML2.DOMDocument
Dim Noeuds As MSXML2.IXMLDOMNodeList, SubNoeuds As MSXML2.IXMLDOMNodeList
Dim balise As IXMLDOMNode, cellule As IXMLDOMElement
Dim Element As IXMLDOMElement, ElementST As IXMLDOMElement, Fonts
Dim i#, F#, A#, ppx#, z$, ids$, innerh$, styles, Attributs, TD, Adr
With CreateObject("WScript.Shell"): ppx = .RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI") / 72: End With
With docxml
xxml = Replace(Replace(Replace(xxml, "ss:Data", "Data"), "<Borders>", ""), "</Borders>", "")
If Not .LoadXML(xxml) Then err.Raise .parseError.ErrorCode, , .parseError.reason
Set Noeuds = docxml.getElementsByTagName("Comment")
For i = 0 To Noeuds.Length - 1
Adr = Noeuds(i).ChildNodes(0).ChildNodes(0).ChildNodes(0).xml
Set TD = doc.getelementById(Adr)
Set cellule = Noeuds(i).ParentNode: If Not IsNull(cellule.getAttribute("ss:StyleID")) Then ids = cellule.getAttribute("ss:StyleID")
TD.iD = ids: TD.classname = Adr
Set Element = Noeuds(i).ParentNode.ChildNodes(0)
If Element.tagName = "Data" Then
innerh = Split(Noeuds(i).ParentNode.ChildNodes(0).xml, "<Data")(1)
z = Split(innerh, ">")(0)
innerh = Replace(Split(Replace(innerh, z & ">", ""), "</Data")(0), "xmlns:html=""http://www.w3.org/TR/REC-html40""", "")
innerh = Replace(Replace(innerh, "xmlns:x=""urn:schemas-microsoft-com:office:excel""", ""), "html:", "")
TD.innerhtml = innerh
If IsDate(Range(Adr)) Then TD.innerhtml = Range(Adr).Text
Set Fonts = TD.getElementsByTagName("FONT")
For F = 0 To Fonts.Length - 1
If Fonts(F).Size <> "" Then Fonts(F).Style.FontSize = Fonts(F).Size * ppx: Fonts(F).Size = "": Fonts(F).removeattribute ("size")
Next
End If
Next
End With
htmltextecell = doc.body.innerhtml
End Function
Sub basetable(doc, plage, xxml)
Dim ppx, TR, TD, Tablo, i#, col#, cel, r, FZ$
With CreateObject("WScript.Shell"): ppx = .RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI") / 72: End With
FZ = val(ThisWorkbook.styles("Normal").font.Size * ppx) & "px"
With doc
.body.innerhtml = "<table><TBODY></TBODY></table>"
With .getElementsByTagName("TABLE")(0):
.Style.Width = (plage.Width + 3) * ppx: .Style.bordercollapse = "collapse": .cellspacing = 0: .Style.FontSize = FZ
r = .setattribute("range", Replace(plage.Address, "$", ""))
End With
Set Tablo = .getElementsByTagName("TBODY")(0)
For i = 1 To plage.Rows.Count
Set TR = .createelement("TR")
r = TR.setattribute("ligne", plage.Cells(i, 1).ROW)
Tablo.appendchild (TR)
For col = 1 To plage.Columns.Count
Set cel = plage.Cells(i, col)
If .getelementById(Replace(cel.MergeArea.Address, "$", "")) Is Nothing Then
cel.AddComment
cel.Comment.Text Text:=Replace(cel.MergeArea.Address, "$", "")
Set TD = .createelement("TD")
TD.iD = Replace(cel.MergeArea.Address, "$", ""): TD.colspan = Range(TD.iD).Columns.Count: TD.rowspan = Range(TD.iD).Rows.Count
r = TD.setattribute("address", TD.iD)
TD.Style.Width = cel.MergeArea.Width * ppx: TD.Style.Height = cel.MergeArea.Height * ppx
'TD.Style.Border = "1px solid rgb(199,199,199)"
TR.appendchild (TD)
End If
Next
Next
End With
With plage
xxml = .Value(xlRangeValueXMLSpreadsheet)
.ClearComments
End With
End Sub |