【问题标题】:Saving an html file from an excel cell从 excel 单元格保存 html 文件
【发布时间】:2020-05-22 10:10:03
【问题描述】:

我正在使用 excel,我有 400 行,其中一列中有我想要的文件名,另一列中有该文件所需的 html 代码。

我想知道是否可以(整体)保存 html 代码的内容,使用关联的文件名,以便每个都是自己的文件?

HTMLtitle,HTMLcode
example1.html,"<!DOCTYPE HTML><html>example 1</html>"
example2.html,"<!DOCTYPE HTML><html>example 2</html>"
example3.html,"<!DOCTYPE HTML><html>example 3</html>"
example4.html,"<!DOCTYPE HTML><html>example 4</html>"

提前致谢!

【问题讨论】:

    标签: html excel saving-data


    【解决方案1】:

    请将以下代码复制到通用模块中,然后运行 ​​CreateBulkHTMLfilesFromExcelRanges 程序并告诉我。

    此方法首先将临时文件创建到临时文件夹中,然后从中读取文本并使用替换功能替换引号。

    Option Explicit
    
    Private Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As LongPtr, ByVal lpbuffer As String) As Long
    
    Private Const MAX_PATH As Long = 260
    
    Sub CreateBulkHTMLfilesFromExcelRanges()
    
        Dim myFile As String, i As Integer
        Dim iFile As Integer: iFile = VBA.FreeFile
        Dim OutputFolder As String
        Dim tmpFile As String
        Dim MyData As String, strData As String
        Dim entireline As String
    
        'We will create a Output Folder Dynamically in the Desktop
        OutputFolder = "C:\Users\" & Environ("UserName") & "\Desktop\Output"
    
        'Check if the output folder exists or not. if it doesn't then it will create one.
        If VBA.Len(VBA.Dir(OutputFolder, vbDirectory)) = 0 Then
            VBA.MkDir OutputFolder
        End If
    
        'Creating Files one by one
        For i = 2 To Sheet1.UsedRange.Rows.Count Step 1
            '~~> Create a Temp File
            tmpFile = TempPath & Format(Now, "ddmmyyyyhhmmss") & ".txt"
            Open tmpFile For Output As #iFile
            Write #iFile, Sheet1.Cells(i, 2).Value
            Close #iFile
    
            '~~> Read the entire file in 1 Go!
            Open tmpFile For Binary As #1
            MyData = Space$(LOF(1))
            Get #1, , MyData
            Close #1
            strData = MyData
    
            '~~> Rewrite file
            myFile = OutputFolder & Application.PathSeparator & Sheet1.Cells(i, 1).Value
            Open myFile For Output As #iFile
            entireline = Replace(strData, """", "")
            Print #iFile, entireline
            Close #iFile
        Next i
    
    End Sub
    
    'Following is the Function to get the temporary folder
    
    Function TempPath() As String
        TempPath = String$(MAX_PATH, Chr$(0))
        GetTempPath MAX_PATH, TempPath
        TempPath = Replace(TempPath, Chr$(0), "")
    End Function
    

    【讨论】:

    • @TommyE:如果我的回答对你有帮助,请点赞。并建议您选择有用的答案。谢谢。
    • 抱歉,我现在又意识到了一件事。当您将其保存为 html 文件时,似乎会在任何 html 现有双引号中添加双引号。知道如何解决这个问题吗?
    • @TommyE:请立即查看代码。它已更新,问题已解决。如果现在还可以,请接受我的回答并勾选有用。谢谢。
    • @TommyE:你检查过更新的答案吗?请检查。
    • 嗨,Kamal,我遇到了一个错误,它说:“必须更新此项目中的代码才能在 64 位系统上使用。请查看并更新 Declare 语句并用 PtrSafe 属性标记它们" - 知道如何解决这个问题吗?
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多