html - 如何使用 VBA 将 Excel 单元格插入到 HTML 类中

标签 html vba excel

是否可以使用 VBA 将 Excel 单元格值插入 HTML 格式?

我有 HTML 模板:

<html>
<head><title></title></head>
<body>
<div class="article">
   <div class="title"></div>
   <div class="date"></div>
   <div class="content"></div>
</div>
</body>
</html>

以及 Excel 中的数据:

|  Title   |  Date   |  Content                             |
+----------+---------+--------------------------------------+
| Sample1  |20150811 | Lorem ipsum dolor                    |
| Sample2  |20150812 | Lorem ipsum dolor                    |
| Sample3  |20150813 | Lorem ipsum dolor                    |

目标:我想创建一个 Excel 宏来将单元格插入到 HTML 类中,并将每一行保存到 HTML 文件中,因此结果是 3 个具有相同模板和不同内容的 HTML。

有谁可以帮我指点一下吗?或者线索,我应该做什么来实现目标。我尝试搜索,但结果是从网络抓取到 Excel。从 Excel 到 HTML 没有任何内容。

最佳答案

这里,我给你准备了一个:

Public Sub exportHTML()

    Dim templateStream As TextStream
    Dim templatePath, templateText, newFile, newText As String
    Dim dataSheet As Worksheet
    Dim row As Integer

    'Create FileSystemObject
    Dim fsObject As New FileSystemObject

    'Set template file path
    templatePath = "C:\template.html"

    'Set sheet
    Set dataSheet = Sheets("sheetname")

    'If template file is exist.
    If fsObject.FileExists(templatePath) Then

        'Open template file
        Set templateStream = fsObject.OpenTextFile(templatePath)

        'Read data
        templateText = templateStream.ReadAll

        'Close template file
        templateStream.Close

        'Looping all row
        For row = 2 To 4 'Here you need to modify the end row as you like

            'Get new html file (filename: Range("A").html)(e.g Sample1.html)
            'You can change file name.
            newFile = ThisWorkbook.Path & "\" & dataSheet.Range("A" & row) & ".html"

            'Set old text to new text
            newText = templateText

            'Set title
            newText = Replace(newText, "<div class=""title""></div>", "<div class=""title"">" & dataSheet.Range("A" & row) & "</div>")

            'Set date
            newText = Replace(newText, "<div class=""date""></div>", "<div class=""date"">" & dataSheet.Range("B" & row) & "</div>")

            'Set content
            newText = Replace(newText, "<div class=""content""></div>", "<div class=""content"">" & dataSheet.Range("C" & row) & "</div>")

            'Create new HTML file and open
            Open newFile For Output As #1

                'Write file content
                Print #1, newText

            'Close new file
            Close

        Next row

    Else
        Call MsgBox("Template HTML file is not exist.", vbExclamation, "Exporting HTML")
    End If

End Sub

我使用以下数据进行了测试:

    +----------+---------+--------------------------------------+
    |    A     |    B    |   C                                  |   
+---+----------+---------+--------------------------------------+
| 1 |  Title   |  Date   |  Content                             |
+---+----------+---------+--------------------------------------+
| 2 | Sample1  |20150811 | Lorem ipsum dolor                    |
| 3 | Sample2  |20150812 | Lorem ipsum dolor                    |
| 4 | Sample3  |20150813 | Lorem ipsum dolor                    |
+---+----------+---------+--------------------------------------+

我在同一目录中得到了三个输出Sample1.html、Sample2.html、Sample3.html,其中包含具有所需内容的 Excel 文件。

关于html - 如何使用 VBA 将 Excel 单元格插入到 HTML 类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31978662/

相关文章:

excel - 对于 VBA,如何使用 IF 语句将特定范围显示为电子邮件的图片?

html - 定位在图片 css html 之上

PHP foreach、CSS 和jQuery 脚本..如何交互?

javascript - 固定标题和可滚动主体

循环浏览工作表时 Excel 宏 : cycle through rows,

vba - VISIO VBA 获取组中所有形状的集合

Excel VBS 中的 RegEx 特定数字模式

vba - 工作表名称中的数字为 "any number"

jquery - 使用 jQuery 将 accesskey 设置为键盘数字键盘键

在数据中添加新列和行时 Excel 会自动更新图表