gpt4 book ai didi

html - 将文本从 Excel 添加到 HTML 文件

转载 作者:太空宇宙 更新时间:2023-11-04 15:45:39 25 4
gpt4 key购买 nike

我正在尝试将 Excel 中的单元格(Testimonial_HTML 工作表中的范围 c3:c14)添加到 html 文件中,并以 HTML 格式保存该文件。

代码(有效地)将文本插入到 HTML 文件的第 92 行,这正是我想要的位置。

代码运行,但是,它没有以 HTML 格式保存。它从原始 html 文件中删除了一些格式。例如,它会删除逗号。

Public Sub FiletoAppend()

Dim inFilePath As String
Dim outFilePath As String
Dim inFile As Integer
Dim outFile As Integer
Dim lineCount As Long
Dim fileLine As String

inFilePath = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\testimonia
ls.html" ' directory of file to append
outFilePath = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\" &
"\MyHTML.html" 'fileName2 = Environ("temp file") & "\MyHTML.html"

inFile = FreeFile
Open inFilePath For Input As #inFile
outFile = FreeFile
Open outFilePath For Output As #outFile
lineCount = 0
Do Until EOF(inFile)
Line Input #inFile, fileLine
lineCount = lineCount + 1
If lineCount < 400 Or lineCount > 401 Then
Print #outFile, fileLine
End If
Loop

Close #inFile
Close #outFile

UpdateHTMLFile

End Sub


Sub UpdateHTMLFile()

' inserts new Testimonial to testimonials.html file at line 92 *** see
below to change line number
'
Sheets("Testimonial_HTML").Activate

Dim line12 As String
Dim line11 As String
Dim line10 As String
Dim line9 As String
Dim line8 As String
Dim line7 As String
Dim line6 As String
Dim line5 As String
Dim line4 As String
Dim line3 As String
Dim line2 As String
Dim line1 As String


Dim MyString As String
Dim fileName As String, fileName2 As String
Dim filenum As Long, filenum2 As Long

Dim i As Long

fileName2 = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\testimonia
ls.html" ' directory of file to append
fileName =
"C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\" &
"\MyHTML.html" 'fileName2 = Environ("temp") & "\MyHTML.html"


line1 = Range("c3:c3") ' the cell that all new jobs are appended to and code copied from
line2 = Range("c4:c4") ' the cell that all new jobs are appended to and code copied from
line3 = Range("c5:c5") ' the cell that all new jobs are appended to and code copied from
line4 = Range("c6:c6") ' the cell that all new jobs are appended to and code copied from
line5 = Range("c7:c7") ' the cell that all new jobs are appended to and code copied from
line6 = Range("c8:c8") ' the cell that all new jobs are appended to and code copied from
line7 = Range("c9:c9") ' the cell that all new jobs are appended to and code copied from
line8 = Range("c10:c10") ' the cell that all new jobs are appended to and code copied from
line9 = Range("c11:c11") ' the cell that all new jobs are appended to and code copied from
line10 = Range("c12:c12") ' the cell that all new jobs are appended to and code copied from
line11 = Range("c13:c13") ' the cell that all new jobs are appended to and code copied from
line12 = Range("c14:c14") ' the cell that all new jobs are appended to and code copied from

filenum2 = FreeFile()
Open fileName2 For Output As #filenum2
filenum = FreeFile()
Open fileName For Input As #filenum
Do While Not EOF(filenum)

i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
n = n + 1
o = o + 1
p = p + 1
q = q + 1
r = r + 1
s = s + 1
t = t + 1

Input #filenum, MyString
Print #filenum2, MyString

If i = 92 Then Print #filenum2, line1 ' i = the line number in html file
If j = 92 Then Print #filenum2, line2 ' j = the line number in html file
If k = 92 Then Print #filenum2, line3 ' k = the line number in html file
If l = 92 Then Print #filenum2, line4 ' l = the line number in html file
If m = 92 Then Print #filenum2, line5 ' m = the line number in html file
If n = 92 Then Print #filenum2, line6 ' n = the line number in html file
If o = 92 Then Print #filenum2, line7 ' o = the line number in html file
If p = 92 Then Print #filenum2, line8 ' p = the line number in html file
If q = 92 Then Print #filenum2, line9 ' q = the line number in html file
If r = 92 Then Print #filenum2, line10 ' r = the line number in html file
If s = 92 Then Print #filenum2, line11 ' s = the line number in html file
If t = 92 Then Print #filenum2, line12 ' t = the line number in html file

Loop
Close #filenum
Close #filenum2

FileCopy fileName2, fileName
Kill fileName

End Sub

如上所述,上述模块运行,但生成的文件不是 HTML 格式,并且缺少逗号和其他一些次要格式。

最佳答案

正如乔在这个问题中所说: why does my vba code see comma as new line?

你可能想使用行输入 #filenum, MyString代替输入#filenum, MyString。

希望这对您有所帮助!

关于html - 将文本从 Excel 添加到 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56576440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com