将十六进制文件转换为Excel文件

3
我正在使用Hanna传感器仪器记录一些数据,这些数据以十六进制代码的形式呈现为.log文件。然后我使用随机附带的Hanna软件将此数据转换为Excel格式(包括数字、字符和特殊字符)。我想知道它是如何做到的,如果可能的话,是否可以不使用该软件自己完成此操作?
该文件看起来像这样,并且其扩展名为.log。
4d65 7465 720a 3230 3137 3039 3139 0000
c1fb 5321 ffff 01fc 1fc8 ffff f4ff f92d
0dc3 58ff 4a00 0000 0000 735a 0081 6101
a242 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 003d 3950 82cc
0940 08b4 3088 c2fb 5321 ffff 01fc 1fc8
ffff f4ff 0b2e 0dae 58ff 4a00 0000 0000
a85a 00a8 6101 1143 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
.
.
.
continued

你的编程问题是什么?从外观上看,你正在问一个硬件问题。传感器如何测量X,信息是如何保存的,以及如何将其从.log转换为.xlsx。 - Bluebird
信息保存为.log文件,然后使用该软件将其转换为Excel表格。我不知道它是如何将.log转换为.xlsx的。我正在尝试找出并自己将其转换为Excel。 - Ritambhara Chauhan
2个回答

1
尝试类似于以下内容:
Sub JustOneFile()
    Dim s As String, i As Long

    i = 1
    Close #1
    Open "C:\TestFolder\james.log" For Input As #1

    Do Until EOF(1)
        Line Input #1, s
        Cells(i, 1) = s
        i = i + 1
    Loop

    Close #1

    Columns("A:A").TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 2), Array(4, 2), Array(9, 2), Array(14, 2), Array(19, 2), _
        Array(24, 2), Array(29, 2), Array(34, 2)), TrailingMinusNumbers:=True
End Sub

enter image description here


0
该过程涉及导入.LOG文件,然后循环遍历所有单元格并逐个进行转换,或将所有数据块存储到变量数组中,进行转换并将转换后的值返回给工作表。建议使用后一种转换方法。
Option Explicit

Sub wqewqw()
    Dim hxwb As Workbook
    Dim i As Long, j As Long, vals As Variant

    Workbooks.OpenText Filename:="%USERPROFILE%\Documents\hex_2_excel.log", _
        Origin:=437, StartRow:=1, DataType:=xlFixedWidth, TrailingMinusNumbers:=True, _
        FieldInfo:=Array(Array(0, 2), Array(4, 2), Array(9, 2), Array(14, 2), _
                         Array(19, 2), Array(24, 2), Array(29, 2), Array(34, 2))
    With ActiveWorkbook
        With .Worksheets(1)
            vals = .Cells(1, "A").CurrentRegion.Cells
            For i = LBound(vals, 1) To UBound(vals, 1)
                For j = LBound(vals, 2) To UBound(vals, 2)
                    vals(i, j) = Application.Hex2Dec(vals(i, j))
                Next j
            Next i
            With .Cells(1, "A").Resize(UBound(vals, 1), UBound(vals, 2))
                .NumberFormat = "General"
                .Value = vals
            End With
        End With
    End With

End Sub

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接