将Excel数据转换为HTML格式的txt文件

时间:2019-09-21 12:58:38

标签: html excel vba

我正在尝试结合excel column1和column2的详细信息以获取文本文件,如图所示,excel最多可以有100行。这是excel数据的图片,暂时限制为3行: enter image description here 这是我要输出的txt格式: enter image description here 我已经开发了代码,但是由于我不是常规的VBA程序员,所以无法实现目标。 这是建议的修改后我当前得到的输出: enter image description here

这是我的代码:

 Dim c As Range, r As Range
    Dim output As String
    Dim txt1 As String
    Dim txt2 As String
    Dim txt3 As String
    Dim txt4 As String
    txt1 = "<div class=" & Chr(34) & "promo_img col-md-4 col-sm-4 col-xs-6" & Chr(34) & ">"
    txt2 = "<!-- PROMOTIONAL IMAGE -->"
    txt3 = "</div>"



    For Each r In Sheet1.Range("A1:C3").Rows
        For Each c In r.Cells
            output = output & "," & c.Value   ' & Chr(10) & Chr(13) & txt2 & Chr(10) & Chr(13) &
        Next c
        output = txt1 & vbNewLine & output & vbNewLine
    Next r
    Open "D:\DOCS\text.txt" For Output As #1
    Print #1, output
    Close
End Sub

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码:

Dim c As Range
Dim output As String
Dim txt1 As String
Dim txt2 As String
Dim txt3 As String
Dim txt4 As String
txt1 = "<div class=" & Chr(34) & "promo_img col-md-4 col-sm-4 col-xs-6" & Chr(34) & ">"
txt2 = "<!-- PROMOTIONAL IMAGE -->"
txt3 = "</div>"

output = ""
For Each c In Sheet1.Range("A1:A3")
    output = output & txt1 & vbnewline & vbnewline & txt2 & vbnewline 
    output = output & "<a href=""" & c.value & """ target=""blank"">"
    output = output & "<img src=""" & c.offset(,1).value & """/></a>"
    output = output & vbnewline & vbnewline & txt3 & vbnewline & vbnewline
Next c

Open "D:\DOCS\text.txt" For Output As #1
Print #1, output
Close #1

vbnewline将更改为换行符
""将返回"