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
|
Sub ExCel_to_fichier_avec_style()
Dim plage As Range, chemin As String
Set plage = Selection
chemin = "C:\Users\" & Environ("UserName") & "\Desktop\" & Replace(plage.Address, ":", "-") & " Avec style " & ".html"
createfichier3 chemin, grille_To_HTML(plage, True)
End Sub
Function récupere_codehtml_de_la_plage_avec_style()
Dim plage As Range
Set plage = Selection
MsgBox grille_To_HTML(plage, True)
End Function
Public Function grille_To_HTML(plage, Optional LsTyLe As Boolean = False) As String
Dim dicorange, codehtml, iedoc As Object, cel, coulnoborder, celhtml, matable, elem, ligne As Long
Dim brTop, brLLeft, brBottom, brRight, SbrTop, SbrLLeft, SbrBottom, SbrRight
Set dicorange = CreateObject("Scripting.Dictionary")
Set iedoc = CreateObject("htmlfile")
codehtml = "<html>" & vbCrLf & "<table>" & vbCrLf & "<tr" & " class= ligne1" & ">" & vbCrLf
ligne = plage.Row
coulnoborder = coul_XL_to_coul_HTMLX(15853019)
For Each cel In plage.Cells
If Not dicorange.exists(cel.MergeArea.Address) Then
dicorange(cel.MergeArea.Address) = ""
If cel.Row <> ligne Then ligne = cel.Row: codehtml = codehtml & vbCrLf & "</tr>" & vbCrLf & "<tr" & _
" class= ligne" & cel.Row & ">" & vbCrLf
codehtml = codehtml & "<td" & " id= " & cel.MergeArea.Address & ">" & cel.Value & "</td>" & vbCrLf
End If
Next
With iedoc
.write codehtml
Set celhtml = .getelementsbytagname("td")
Set matable = .getelementsbytagname("table")(0)
With matable: .cellpadding = 0: .cellspacing = 0:
'.Style.bordercollapse = "collapse"
.classname = Replace(Replace(plage.Address, ":", "-"), "$", "")
End With
For Each elem In celhtml
elem.rowspan = Range(elem.ID).Rows.Count
elem.colspan = Range(elem.ID).Columns.Count
elem.Style.Width = Range(elem.ID).Width / (3 / 4)
elem.Style.Height = Range(elem.ID).Height / (3 / 4)
elem.Style.FontSize = Range(elem.ID).Font.Size * 1.3
elem.Style.Border = 1 & " solid " & coulnoborder
If Range(elem.ID).HorizontalAlignment <> 1 Then elem.Style.TextAlign = "center"
If Range(elem.ID).VerticalAlignment = xlBottom Then elem.Style.verticalalign = "bottom"
If LsTyLe = False Then
matable.Style.Border = 1 & " solid " & "gray" 'coulnoborder
Else
elem.Style.Border = 0
elem.Style.fontweight = IIf(Range(elem.ID).Font.Bold, "Bold", "normal")
elem.Style.fontFamily = Range(elem.ID).Font.Name
elem.Style.FontStyle = IIf(Range(elem.ID).Font.Italic = True, "italic", "normal")
elem.Style.BackgroundColor = coul_XL_to_coul_HTMLX(Range(elem.ID).Interior.Color)
elem.Style.Color = coul_XL_to_coul_HTMLX(Range(elem.ID).Font.Color)
SbrTop = borders(Range(elem.ID).borders(xlEdgeTop)).lestyle
SbrBottom = borders(Range(elem.ID).borders(xlEdgeBottom)).lestyle
SbrRight = borders(Range(elem.ID).borders(xlEdgeRight)).lestyle
SbrLLeft = borders(Range(elem.ID).borders(xlEdgeLeft)).lestyle
brTop = borders(Range(elem.ID).borders(xlEdgeTop)).leWeights
brBottom = borders(Range(elem.ID).borders(xlEdgeBottom)).leWeights
brRight = borders(Range(elem.ID).borders(xlEdgeRight)).leWeights
brLLeft = borders(Range(elem.ID).borders(xlEdgeLeft)).leWeights
If Range(elem.ID).Row = plage.Row And Range(elem.ID).borders(xlEdgeTop).LineStyle <> xlNone Then elem.Style.BorderTop = brTop & " " & SbrTop & " " & coul_XL_to_coul_HTMLX(Range(elem.ID).borders(xlEdgeTop).Color)
If Range(elem.ID).Column = plage.Column And Range(elem.ID).borders(xlEdgeLeft).LineStyle <> xlNone Then elem.Style.Borderleft = brLLeft & " " & SbrLLeft & " " & coul_XL_to_coul_HTMLX(Range(elem.ID).borders(xlEdgeLeft).Color)
If Range(elem.ID).borders(xlEdgeBottom).LineStyle <> xlNone Then elem.Style.Borderbottom = brBottom & " " & SbrBottom & " " & coul_XL_to_coul_HTMLX(Range(elem.ID).borders(xlEdgeBottom).Color)
If Range(elem.ID).borders(xlEdgeRight).LineStyle <> xlNone Then elem.Style.Borderright = brRight & " " & SbrRight & " " & coul_XL_to_coul_HTMLX(Range(elem.ID).borders(xlEdgeRight).Color)
If Range(elem.ID).Row = plage.Row And Range(elem.ID).borders(xlEdgeTop).LineStyle = xlNone Then elem.Style.BorderTop = 1 & " solid" & " " & coulnoborder
If Range(elem.ID).Column = plage.Column And Range(elem.ID).borders(xlEdgeLeft).LineStyle = xlNone Then elem.Style.Borderleft = 1 & " solid" & " " & coulnoborder
If Range(elem.ID).borders(xlEdgeBottom).LineStyle = xlNone Then elem.Style.Borderbottom = 1 & " solid" & " " & coulnoborder
If Range(elem.ID).borders(xlEdgeRight).LineStyle = xlNone Then elem.Style.Borderright = 1 & " solid" & " " & coulnoborder
End If
Next
grille_To_HTML = .body.innerhtml
End With
End Function
Function borders(Cote) As propert
borders.leWeights = Switch(Cote.Weight = 1, 1, Cote.Weight = 2, 1, Cote.Weight = -4138, 2, Cote.Weight = 4, 3)
borders.lestyle = IIf(Cote.LineStyle <> 1, "dashed", "solid")
'If Cote.LineStyle = xlDashDotDot Then borders.leWeights = 3 'xlDashDotDot
If Cote.LineStyle = xlDash And Cote.Weight = xlThick Then borders.leWeights = 2 ' xldash et epaisseur xlthick 'Tiret en pointillet
If Cote.LineStyle = xlDash And Cote.Weight = xlThin Then borders.leWeights = 2:: borders.lestyle = "dotted" 'point en pointillet
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
Public Sub createfichier3(chemin, texte)
Dim intFic As Integer
intFic = FreeFile
Open chemin For Output As intFic
Print #intFic, texte
Close intFic
End Sub |