从 excel 单元格保存 html 文件
Saving an html file from an excel cell
我正在使用 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>"
提前致谢!
请将以下代码复制到通用模块和 运行 过程 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
我正在使用 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>"
提前致谢!
请将以下代码复制到通用模块和 运行 过程 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