2017-10-19 7 views
0

Quelqu'un peut-il m'aider à rendre ce code viable?VBA Contenu Excel en HTML (créer un fichier HTML à partir d'Excel)

L'idée ici est de rassembler une ligne d'ensemble avec des informations sur différentes lignes et d'insérer cette information dans un fichier html.

Quelques avantages sur le code qui me ferait très reconnaissant:

1) faire fonctionner 8P; 2) une sorte de façon pour l'utilisateur de choisir l'endroit où il va enregistrer le fichier comme une fenêtre de sauvegarde normale (si ce n'est pas possible, laissez-le au moins choisir le nom du fichier dans le dossier assigné); et 3) s'assurer que le code capture toutes les lignes non vides sur la ligne.

Un grand merci à la communauté pour l'attention!

Ce dont je suis venu est ci-dessous.

Sub CreateHTML() 
'Define your variables. 
    Dim iRow As Long 
    Dim iStage As Integer 
    Dim iCounter As Integer 
    Dim iPage As Integer 

    'Create an .htm file in the assigned directory. 
    Dim sFile As String 
    sFile = "J:\GEROC1\Avaliação RO\4) CICLOS\ArquivosExportados" & "\test.html" 
    Close 

    'Open up the temp HTML file and format the header. 
    Open sFile For Output As #1 
    Print #1, "<html>" 
    Print #1, "<head>" 
    Print #1, "<style type=""text/css"">" 
    Print #1, "table {font-size: 16px;font-family: Optimum, Helvetica, sans-serif;Border -collapse: collapse}" 
    Print #1, "tr {border-bottom: 1px solid #A9A9A9;}" 
    Print #1, "td {padding: 4px; margin: 3px; padding-left: 20px; width: 75%; text-align: justify;}" 
    Print #1, "th { background-color: #A9A9A9; color: #FFF; font-weight: bold; font-size: 28px; text-align: center;}" 
    Print #1, "</style>" 
    Print #1, "</head>" 
    Print #1, "<body>" 
    Print #1, "<table class=""table""><thead><tr class=""firstrow""><th colspan=""2"">Ficha de Risco</th></tr></thead><tbody>" 

    'Start on the 2nd row to avoid the header. 
    iRow = 2 

    'Translate the first column of the table into the first level of the hierarchy. 
    Do While WorksheetFunction.CountA(Rows(iRow)) > 0 
     If Not IsEmpty(Cells(iRow, 23)) Then 
     For iCounter = 1 To iStage 
      'Print #1, "</ul>" 
      iStage = iStage - 1 
     Next iCounter 
     Print #1, Cells(iRow, 1).Value 
     iPage = iPage + 1 
     If iStage < 1 Then 
      iStage = iStage + 1 
     End If 
     End If 
    Loop 

    'Add ending HTML tags 
    Print #1, "</body>" 
    Print #1, "</html>" 
    Close 
End Sub 
+0

Vous n'ajoutez aucun code HTML dans votre boucle. Vous devez ajouter la table 'tr' et' td' avec du contenu dedans. – Patrick

+0

Ops j'ai oublié de mentionner ... les cellules qu'il imprime ont déjà les déclarations tr et td ... – PunchTheNewbie

Répondre

0

Le code ci-dessous crée un fichier html à partir d'une table dans votre fichier Excel. Il écrira la table entière et recherchera le nombre de lignes et de colonnes pour écrire tout ce qui est en train de s'afficher.

Veuillez vous assurer de modifier la section html de la table elle-même. Dans mon exemple, je voulais transposer des lignes et des colonnes et les imprimer une à une.

De même, assurez-vous de régler si vous voulez un en-tête principal de table avec un titre. N'oubliez pas d'ajuster le colspan.

Cet exemple peut être modifié pour écrire n'importe quelle table dans un fichier html. Enfin, je mets un peu plus où il vous demandera où vous voulez sauvegarder le fichier.

Sub CreateHTML() 
    'Define your variables. 
    Dim iRow As Long 
    Dim tRow As Long 
    Dim iStage As Integer 
    Dim iCounter As Integer 
    Dim iPage As Integer 
    Dim lastCol As Integer 
    Dim lastRow As Integer 

    'Find the last Column Number 
    With ActiveSheet 
     lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    End With 

    'Find the last Column Row 
    With ActiveSheet 
     lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 

    'Create an .html file in the assigned directory. 
    Dim sFile As Variant 

    sFile = Application.GetSaveAsFilename(fileFilter:="HTML Files (*.html), *.htm") 

    If fileName <> False Then 
    SaveWorkbook = fileName 
    End If 

    'Open up the temp HTML file and format the header. 
    Open sFile For Output As #1 
    Print #1, "<html>" 
    Print #1, "<head>" 
    Print #1, "<style type=""text/css"">" 
    Print #1, "table {font-size: 16px;font-family: Optimum, Helvetica, sans-serif; border-collapse: collapse}" 
    Print #1, "tr {border-bottom: thin solid #A9A9A9;}" 
    Print #1, "td {padding: 4px; margin: 3px; padding-left: 20px; width: 75%; text-align: justify;}" 
    Print #1, "th { background-color: #A9A9A9; color: #FFF; font-weight: bold; font-size: 28px; text-align: center;}" 
    Print #1, "td:first-child { font-weight: bold; width: 25%;}" 
    Print #1, "</style>" 
    Print #1, "</head>" 
    Print #1, "<body>" 
    Print #1, "<table class=""table""><thead><tr class=""firstrow""><th colspan=""2"">INSERT TABLE MAIN HEADER HERE - WATCH OUT FOR TABLE COLSPAN</th></tr></thead><tbody>" 

    'Translate the first column of the table into the first level of the hierarchy. 
    tRow = 1 

    'Start on the 2nd row to avoid the header. - iRow=2/tRow is the table header 
    For iRow = 2 To lastRow 


    For iCounter = 1 To lastCol 

    'EDIT HERE TO CHANGE IT TO YOUR LINKING 
    Print #1, "<tr><td>" 
    Print #1, Cells(tRow, iCounter).Value 
    Print #1, "</td><td>" 
    Print #1, Cells(iRow, iCounter).Value 
    Print #1, "</td></tr>" 

    Next iCounter 
    Next iRow 

    'Add ending HTML tags 
    Print #1, "</body>" 
    Print #1, "</html>" 
    Close 
    End Sub