html - 将html数据导入Excel时如何显示阿拉伯字母?
问题描述
我在这里阅读了一个关于将 html 转换为 Excel 的问题,它可以工作,但有未解决的问题。如果 html 包含阿拉伯字母,因为它在导入后无法在 Excel 中正确显示。
- 我复制的导入代码如下
- 我尝试使用以下代码修复字符集问题,但出现运行时错误
HTML_Content.Charset = "utf-8"
(在下面的“创建 HTMLFile 对象”部分中)
Sub HTML_Table_To_Excel()
Dim htm As Object
Dim Tr As Object
Dim Td As Object
Dim Tab1 As Object
Dim file As String
'Replace the file path with your own
file = "c:\your_File.html"
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file
Open file For Input As TextFile
'Create HTMLFile Object
Set HTML_Content = CreateObject("htmlfile")
HTML_Content.body.innerHTML = Input(LOF(TextFile), TextFile)
Column_Num_To_Start = 1
iRow = 2
iCol = Column_Num_To_Start
iTable = 0
'Loop Through Each Table and Download it to Excel in Proper Format
For Each Tab1 In HTML_Content.getElementsByTagName("table")
With HTML_Content.getElementsByTagName("table")(iTable)
For Each Tr In .Rows
For Each Td In Tr.Cells
Sheets(1).Cells(iRow, iCol).Select
Sheets(1).Cells(iRow, iCol) = Td.innerText
iCol = iCol + 1
Next Td
iCol = Column_Num_To_Start
iRow = iRow + 1
Next Tr
End With
iTable = iTable + 1
iCol = Column_Num_To_Start
iRow = iRow + 1
Next Tab1
MsgBox "Process Completed"
End Sub
解决方案
charSet
是一个只读属性。如果尝试通过此属性进行设置,这可能是您的错误的根源。
我想你可能想要:
HTML_Content.defaultCharset = "UTF-8"
我更愿意MSHTML.HTMLDocument
通过 VBE > Tools >References > Microsoft HTML Object Library reference 声明 early bound,然后像html.defaultCharset = "UTF-8"
where html
is your instance of the object。
我真的不喜欢htmlFile
它,因为它是如此有限,并且仅在您绝对无法添加项目引用时才有用。
另外,请声明所有变量并Option Explicit
在模块顶部使用。
推荐阅读
- javascript - 克隆 html 表单标签后不显示图像预览
- if-statement - 在 redis 调用上使用 luascript 检查 nil 不起作用
- reporting-services - 如何根据控制中断在标题中打印文本或图像
- python - Pandas - 从列中提取多个数据
- linux - grepping后如何过滤部分行
- sql - Getdate() 函数在 SQL Server 2014 Express 中不起作用
- python - 在控制台中打印,但在格式化为 CSV 时不打印
- asp.net-web-api - 通过 Axios.js 库调用 API 端点时 MessageHandler 未正确执行
- r - 如何通过 2 个变量对 ggplot boxplot 点进行颜色编码?
- css - IE 11 对 CSS 网格的支持