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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| Sub TESTENDOM3()
Dim PLAGE As Range, code As String, IE As Object
Set PLAGE = Sheets(1).Range("A4:C" & Cells(Rows.Count, 1).End(xlUp).Row) 'adqapter la plage ici
code = range_to_html_DOM3(PLAGE, True)
Set IE = CreateObject("internetexplorer.application")
With IE
.Visible = True: .Left = 100: .Navigate "about:blank" ' la position de la fenetre ie est en dehors du chanps de l'ecran (pas la peine dela voir ca n'est pas necessaire
.Document.write code
End With
End Sub
Function range_to_html_DOM3(PLAGE, Optional en_couleur As Boolean = False) As String
Dim TD, TR, Doc, Table, cel As Range, V, TA, VRL, TAL, i As Long, lig As Long, col As Long, mesTD, B As Boolean
Dim rangeleft As Range, rangeTOP As Range, rangeBOTTOM As Range, RL, R
Set Doc = CreateObject("htmlfile")
With Doc
.write "<Table id=""tabl"" cellspacing=15 cellpadding=0 style=""table-layout: fixed;border-collapse:collapse;""></table>"
Set Table = .getelementbyid("tabl"): Table.Style.Width = Round(PLAGE.Width * 1.75) & "px":
For lig = PLAGE.Row To PLAGE.Row + (PLAGE.Rows.Count - 1)
Set TR = .createElement("TR")
Table.appendchild (TR)
For col = PLAGE.Column To PLAGE.Column + (PLAGE.Columns.Count - 1)
Set cel = Cells(lig, col).MergeArea
If .getelementbyid(cel.Address) Is Nothing Then
Set TD = .createElement("TD")
TD.ID = cel.Address: TD.rowspan = cel.Rows.Count: TD.colspan = cel.Columns.Count: TD.Style.Border = "0.1pt solid #CED8F6"
TD.Style.Width = Round(cel.Width * 1.7) & "px": TD.Style.Height = Round(cel.Height * 1.6666666) & "px"
If cel.WrapText = True Then TD.Style.WordWrap = "break-word"
V = cel.VerticalAlignment: VRL = Switch(V = xlTop, "top", V = xlBottom, "bottom", V = xlCenter, "middle"): TD.Style.verticalAlign = VRL
TA = cel.HorizontalAlignment: TAL = Switch(TA = xlLeft, "left", TA = xlCenter, "center", TA = xlRight, "right"): If Not IsNull(TAL) Then TD.Style.textAlign = TAL
If en_couleur Then
If cel.Cells(1).Value <> "" Then TD.innerhtml = text_formaté(cel.Cells(1))
TD.Style.bordertop = borderstyle(cel.Borders(xlEdgeTop))
TD.Style.borderleft = borderstyle(cel.Borders(xlEdgeLeft))
TD.Style.borderbottom = borderstyle(cel.Borders(xlEdgeBottom))
TD.Style.borderright = borderstyle(cel.Borders(xlEdgeRight))
TD.Style.backgroundcolor = coul_XL_to_coul_HTMLX(cel.Interior.Color)
Else
TD.innerhtml = IIf(cel.Cells(1).Value <> "", cel.Cells(1).Value, "")
End If
'corection des marginright et marginleft du texte dans les cellules HTML
If TD.Children.Length > 0 Then
For i = 1 To TD.Children.Length - 1
TD.Children(i).Style.margin = "0.5pt"
Next
TD.LastChild.Style.MarginRight = "3px": TD.FirstChild.Style.MarginLeft = "3px"
Else
If TD.innertext <> "" Then TD.FirstChild.Style.MarginRight = "3px"
End If
End If
TR.appendchild (TD)
Next
Next
'correction des bordure gauche et droite en cas de fusion sur plusieurs lignes
Set mesTD = .getelementsbytagname("TD")
For i = 0 To mesTD.Length - 1
'gauche et droite en cas de fusion sur plusieurs lignes
If Range(mesTD(i).ID).Rows.Count > 1 And Range(mesTD(i).ID).Column > PLAGE.Column Then
B = True
Set rangeleft = Range(mesTD(i).ID).Offset(0, -1).Resize(Range(mesTD(i).ID).Rows.Count, 1)
For RL = 2 To rangeleft.Cells.Count
If rangeleft.Cells(RL).Borders(xlEdgeRight).LineStyle <> rangeleft.Cells(RL - 1).Borders(xlEdgeRight).LineStyle Then B = False
Next
If B = True Then
For Each cel In rangeleft.Cells
.getelementbyid(cel.MergeArea.Address).Style.borderright = 0
Next
Else
mesTD(i).Style.borderleft = 0
End If
End If
'top et basse en cas de fusion sur plusieurs colonne
If Range(mesTD(i).ID).Columns.Count > 1 And Range(mesTD(i).ID).Column >= PLAGE.Column Then
B = True
Set rangeBOTTOM = Range(mesTD(i).ID).Offset(1, 0).Resize(1, Range(mesTD(i).ID).Columns.Count)
Set rangeTOP = Range(mesTD(i).ID).Offset(-1, 0).Resize(1, Range(mesTD(i).ID).Columns.Count)
For RL = 2 To rangeBOTTOM.Cells.Count
If rangeBOTTOM.Cells(RL).Borders(xlEdgeTop).LineStyle <> rangeBOTTOM.Cells(RL - 1).Borders(xlEdgeTop).LineStyle Then B = False
Next
If B = True Then
For Each cel In rangeleft.Cells: .getelementbyid(cel.MergeArea.Address).Style.bordertop = 0: Next
Else
mesTD(i).Style.borderbottom = 0
End If
For RL = 2 To rangeTOP.Cells.Count
If rangeTOP.Cells(RL).Borders(xlEdgeBottom).LineStyle <> rangeTOP.Cells(RL - 1).Borders(xlEdgeBottom).LineStyle Then B = False
Next
If B = True Then
For Each cel In rangeleft.Cells: .getelementbyid(cel.MergeArea.Address).Style.borderbottom = 0: Next
Else
mesTD(i).Style.bordertop = 0
End If
End If
Next
For i = 0 To mesTD.Length - 1
If mesTD(i).colspan = 1 And mesTD(i).rowspan = 1 And Range(mesTD(i).ID).Row > PLAGE.Row Then
Set R = Range(mesTD(i).ID).Offset(-1, 0).MergeArea
If .getelementbyid(R.Address).Style.borderbottom = mesTD(i).Style.bordertop Then mesTD(i).Style.bordertop = 0
End If
Next
For i = 0 To mesTD.Length - 1
If mesTD(i).colspan = 1 And mesTD(i).rowspan = 1 And Range(mesTD(i).ID).Column > PLAGE.Column Then
Set R = Range(mesTD(i).ID).Offset(0, -1).MergeArea
Debug.Print R.Address
If .getelementbyid(R.Address).Style.borderright = mesTD(i).Style.borderleft Then mesTD(i).Style.borderleft = 0
End If
Next
'Debug.Print .body.innerhtml
range_to_html_DOM3 = .body.innerhtml
End With
End Function
Function text_formaté(cel)
Dim F, Doc, L, formt, mot, forma, font, i As Long
Set Doc = CreateObject("htmlfile")
Doc.write "<br><div id=""mot""></div>"
F = ""
With Doc
Set mot = .getelementbyid("mot")
If IsDate(cel.Value) Then
forma = cel.NumberFormat
Set font = .createElement("FONT")
font.innerhtml = IIf(cel.font.Italic, "<em>" & Format(cel.Value, forma) & "</em>", Format(cel.Value, forma))
font.innerhtml = IIf(cel.font.Bold, "<strong>" & font.innerhtml & "</strong>", Format(cel.Value, forma))
font.Color = coul_XL_to_coul_HTMLX(cel.font.Color): font.face = cel.font.Name: font.Size = Round(cel.font.Size / 3) & "px "
text_formaté = font.outerhtml: Exit Function
End If
If IsNumeric(cel.Value) Then
Set font = .createElement("FONT")
font.innerhtml = IIf(cel.font.Italic, "<em>" & cel.Value & "</em>", cel.Value)
font.innerhtml = IIf(cel.font.Bold, "<strong>" & font.innerhtml & "</strong>", cel.Value)
font.Color = coul_XL_to_coul_HTMLX(cel.font.Color): font.face = cel.font.Name: font.Size = Round(cel.font.Size / 3) & "px "
text_formaté = font.outerhtml: If cel.NumberFormat <> "@" Then Exit Function
End If
For i = 1 To Len(cel.Value)
L = CStr(cel.Characters(Start:=i, Length:=1).Text)
formt = "size=" & Round(cel.Characters(Start:=i, Length:=1).font.Size / 3) & "pt " & "face=""" & cel.Characters(Start:=i, Length:=1).font.Name & Chr(34) & " "
formt = formt & "color=""" & coul_XL_to_coul_HTMLX(cel.Characters(Start:=i, Length:=1).font.Color) & Chr(34) & ">"
If F <> formt Then
Set font = .createElement("FONT"): F = formt
font.Color = coul_XL_to_coul_HTMLX(cel.Characters(Start:=i, Length:=1).font.Color)
font.face = cel.Characters(Start:=i, Length:=1).font.Name
font.Size = Round(cel.Characters(Start:=i, Length:=1).font.Size / 3) & "px "
End If
If cel.Characters(Start:=i, Length:=1).font.Italic = True Then L = "<em>" & L & "</em>"
If cel.Characters(Start:=i, Length:=1).font.Bold = True Then L = "<strong>" & L & "</strong>"
font.innerhtml = font.innerhtml & L
mot.appendchild (font)
Next
text_formaté = Replace(mot.innerhtml, "</STRONG><STRONG>", "")
'Debug.Print Replace(mot.innerhtml, "</STRONG><STRONG>", "")
End With
Set Doc = Nothing
End Function
Function coul_XL_to_coul_HTMLX(couleur)
Dim str0 As String, str As String
'If couleur = 16777215 Then couleur = vbWhite
str0 = Right("000000" & Hex(couleur), 6)
str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
coul_XL_to_coul_HTMLX = "#" & str & ""
End Function
Function borderstyle(Cote)
Dim borderweight, bstyle As String, bcolor As String
If Cote.LineStyle = xlNone Then borderstyle = "0.1pt solid #CED8F6": GoTo fin
borderweight = Switch(Cote.Weight = 1, 1, Cote.Weight = 2, 1, Cote.Weight = -4138, 2, Cote.Weight = 4, 3) & "px "
bstyle = IIf(Cote.LineStyle <> 1, "dashed", "solid") 'xlDouble
If Cote.LineStyle = xlDash And Cote.Weight = xlThick Then borderweight = 3 & "px " ' xldash et epaisseur xlthick 'Tiret en pointillet
If Cote.LineStyle = xlDash And Cote.Weight = xlThin Then borderweight = 2 & "px ":: bstyle = "dotted" 'point en pointillet
If Cote.LineStyle = xlDashDotDot Then bstyle = " dashed": borderweight = 3 & "px " 'xlDashDotDot
If Cote.LineStyle = xlDouble Then bstyle = " double": borderweight = 3 & "px "
bcolor = coul_XL_to_coul_HTMLX(Cote.Color)
borderstyle = borderweight & bstyle & " " & bcolor
fin:
End Function |
Partager