在Excel VBA中从UCS2 Little Endian转换为ANSI

在一些testing中,我正在运行,我有一些自动生成的.htm文件。 我一直试图parsing数据,并把它带入excel。 我终于意识到问题是实验室设备输出的文件是用UCS-2 Little Endian编码的。 为了将文件读入文本stream,它需要是ANSI或Unicode。

有没有一种方法,我可以在两个编码之间转换,而不使用外部应用程序?

谢谢。

编辑:
所以我试着按照Noodles的回答,用StrConv来实现一个解决scheme。 但是,它似乎并没有像我所期望的那样工作。 以下是我的代码。 有没有人有任何想法,为什么它不工作?

Sub ParseReport() 'Parses TX Compliance report generated by LeCroy Scope Application.ScreenUpdating = False 'Turn off screen updating until end of sub On Error Resume Next 'ignore errors Dim fso As New FileSystemObject Set rStart = Range("A1") 'set starting cell to A0 sPath = Range("B3").Value & "\" 'set file path (pulled from cell B3) sPart = ActiveSheet.Name 'set part name to be active sheet name sEnd = "<" 'set end string to "<" (used in for loops below to pull result values from file) For iVT = 0 To 24 Step 6 'loop through all VT runs (hilighted in yellow in worksheet) For iPort = 0 To 4 'loop through all ports within each VT parameter sVT = rStart.Offset(4, 1 + iVT).Value 'get VT parameter for filename sPort = rStart.Offset(5, 1 + iVT + iPort).Value 'get port information for filename sFileName = sPath & sPart & "_" & sPort & "_" & sVT & ".htm" 'set filename Set sFile = fso.OpenTextFile(sFileName, ForReading, TristateTrue) 'open file in text stream as unicode sFileText = sFile.ReadAll 'parse entire file into string sAnsiFile = StrConv(sFileText, vbFromUnicode) 'convert from unicode to system default File.Close 'close text stream Debug.Print sFileText For iTest = 0 To 52 'loop through each test run on part sTest = rStart.Offset(6 + iTest, 0).Value 'get test name from worksheet iBegin = InStr(sFileText, sTest) + Len(sTest) + 28 'set beginning character index for string parsing iEnd = InStr(iBegin, sFileText, sEnd) 'set ending character index for string parsing sValue = Mid(sFileText, iBegin, iEnd - iBegin) 'extract result data from file text rTest.Offset(iTest, iVT + iPort + 1) = sValue 'place result data in appropriate cell Next 'next test Next 'next port Next 'next VT run Application.ScreenUpdating = True 'turn on screen updating End Sub 

目前,stringsFileText和sAnsiFile(我最终想要分析并将其带入工作表)没有被从.htm文件中填充整个文本。 当我运行代码时,sFileText和sAnsiFile的本地输出如下所示:

sFileText:“ÿþ<:Variant / String
sUniFile:“?<:Variant / String

在此先感谢您的帮助。

EDIT2:
对我来说这么大的疏忽:我忘了.htm文件在标题中包含编码。 我正在使用的文件是UTF-16。 我不确定这是否有所作为。

谢谢。

EDIT3:
好的,所以我select了面条的答案,因为他回答了我的问题。 然而,这并没有解决我的问题,主要是因为我认为我是在问错误的问题。 无论如何,在我的评论下面,我注意到解决我的问题的解决scheme(由于某些原因,在.OpenTextFile方法中添加“False”作为参数使其工作)。 有了这个修复,StrConv()函数并不是真正需要的。 以下是更新的代码。

 Sub ParseReport() 'Parses TX Compliance report generated by LeCroy Scope Application.ScreenUpdating = False 'Turn off screen updating until end of sub On Error Resume Next 'ignore errors Dim fso As New FileSystemObject Set rStart = Range("A1") 'set starting cell to A0 sPath = Range("B3").Value & "\" 'set file path (pulled from cell B3) sPart = ActiveSheet.Name 'set part name to be active sheet name sEnd = "<" 'set end string to "<" (used in for loops below to pull result values from file) For iVT = 0 To 24 Step 6 'loop through all VT runs (hilighted in yellow in worksheet) For iPort = 0 To 4 'loop through all ports within each VT parameter sVT = rStart.Offset(4, 1 + iVT).Value 'get VT parameter for filename sPort = rStart.Offset(5, 1 + iVT + iPort).Value 'get port information for filename sFileName = sPath & sPart & "_" & sPort & "_" & sVT & ".htm" 'set filename Set sFile = fso.OpenTextFile(sFileName, ForReading, False, TristateTrue) 'open file in text stream as unicode sFileText = sFile.ReadAll 'parse entire file into string File.Close 'close text stream Debug.Print sFileText For iTest = 0 To 52 'loop through each test run on part sTest = rStart.Offset(6 + iTest, 0).Value 'get test name from worksheet iBegin = InStr(sFileText, sTest) + Len(sTest) + 28 'set beginning character index for string parsing iEnd = InStr(iBegin, sFileText, sEnd) 'set ending character index for string parsing sValue = Mid(sFileText, iBegin, iEnd - iBegin) 'extract result data from file text rTest.Offset(iTest, iVT + iPort + 1) = sValue 'place result data in appropriate cell Next 'next test Next 'next port Next 'next VT run Application.ScreenUpdating = True 'turn on screen updating End Sub 

感谢所有帮助过的人。

StrConv函数

返回按指定转换的Variant(String)。

句法

 StrConv(string, conversion, LCID) 

StrConv函数语法具有这些命名参数:

部分说明

string必需。 要转换的stringexpression式。

转换必需 。 整数。 指定要执行的转换types的值的总和。

LCID可选 。 LocaleID,如果不同于系统LocaleID。 (系统的LocaleID是默认的。)

设置

转换参数设置是:

常量:值 – 说明

  • vbUpperCase :1 – 将string转换为大写字符。
  • vbLowerCase :2 – 将string转换为小写字符。
  • vbProperCase :3 – 将string中每个单词的第一个字母转换为大写。
  • vbWide :4 – 将string中的窄(单字节)字符转换为宽(双字节)字符
  • vbNarrow :8 – 将string中的宽(双字节)字符转换为窄(单字节)*字符
  • vbKatakana :16 – 将string中的平假名字符转换为片假名字符。
  • vbHiragana :32 – 将string中的片假名字符转换为平假名字符。
  • vbUnicode :64 – 使用系统的默认代码页将string转换为Unicode。
  • vbFromUnicode :128 – 将string从Unicode转换为系统的默认代码页。

我失去了我的其他身份证,所以我是一个新的面条。

你的第一个代码想工作的原因就是这么说的

 Set sFile = fso.OpenTextFile(sFileName, ForReading, TristateTrue) 

这意味着fname = sFilename ,打开模式= ForReading ,CREATE = -1(TriStateTrue)没有指定可选的格式 。 创build采用True或False,所以你指定创build文件,如果它不存在,并打开ANSI默认(我知道它说ASCII,但Windows使用ANSI)。

你可以做的是省略参数(寻找额外的逗号)。

 Set sFile = fso.OpenTextFile(sFileName, ForReading, , TristateTrue) 

我注意到你使用这个

 Application.ScreenUpdating = False 

所以如果你对性能感兴趣,最小化点(每个点都是查找),特别是在循环中。

有两种方法。 一个是设置对象到最低的对象,如果你要访问不止一次。

例如(较慢)

 set xlapp = CreateObject("Excel.Application") msgbox xlapp.worksheets(0).name 

(更快,因为每次使用对象时都省略一个点)

 set xlapp = CreateObject("Excel.Application") set wsheet = xlapp.worksheets(0) msgbox wsheet.name 

第二种方法是with 。 一次只能有一个活动。

这跳过了100个查找。

 with wsheet For x = 1 to 100 msgbox .name Next end with