如何用VBA将Excel单元格插入HTML类

如何用VBA将Excel单元格插入HTML类,html,vba,excel,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="conten

是否可以使用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宏,将单元格插入到HTML类中,并将每一行保存到HTML文件中,因此结果是3个具有相同模板和不同内容的HTML


有人能帮我指出一些参考资料吗?或者,我应该做些什么来实现目标。我试着搜索,但结果是从web抓取到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

我在excel文件的同一目录中获得了三个输出,分别是Sample1.html、Sample2.html和Sample3.html,并包含了所需的内容。

您可以使用CSS选择器更新html文档中的元素,如下所示:

Option Explicit
Public Sub AddHMTLInfo()
    Dim htmlText As String, html As HTMLDocument '<== Requires reference to HTML Object Library
    htmlText = [A1]                              '<==Reading you sample in from cell
    If htmlText = vbNullString Then Exit Sub
    Set html = New HTMLDocument
    With html
        .body.innerHTML = htmlText
        .querySelector(".title").innerText = "myTitle"
        .querySelector(".date").innerText = "myDate"
        .querySelector(".content").innerText = "myContent"
        Debug.Print .body.innerHTML                   '<== Verify changes
    End With
End Sub
选项显式
公共子AddHMTLInfo()

Dim htmlText作为字符串,html作为HTMLDocument'hi。。Nicolas感谢您的回答,当我尝试时,错误窗口出现,并说“用户定义类型未定义”和“Dim templateStream As TextStream”是否有任何库支持TextStream或什么?upss。。很抱歉,对于错误“用户定义的类型不是定义”,它是使用“Microsoft脚本运行时”解决的,但现在出现了新错误“运行时错误”91:对象变量或未设置块变量”,哪一行给出了错误。我已经测试过了,它对我来说很好。调试器指向这一行“templateText=templateStream.ReadAll”,我在使用excel宏方面是新手,可能我错过了Ms excel的一些配置。如果添加引用,则无法显示错误。我建议重新启动excel应用程序并重新运行它。
    +----------+---------+--------------------------------------+
    |    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                    |
+---+----------+---------+--------------------------------------+
Option Explicit
Public Sub AddHMTLInfo()
    Dim htmlText As String, html As HTMLDocument '<== Requires reference to HTML Object Library
    htmlText = [A1]                              '<==Reading you sample in from cell
    If htmlText = vbNullString Then Exit Sub
    Set html = New HTMLDocument
    With html
        .body.innerHTML = htmlText
        .querySelector(".title").innerText = "myTitle"
        .querySelector(".date").innerText = "myDate"
        .querySelector(".content").innerText = "myContent"
        Debug.Print .body.innerHTML                   '<== Verify changes
    End With
End Sub