如何将BIN文件转换成EXCEL文件,并可以修改保存
本帖最后由 jingningmeng 于 2013-07-17 19:17:58 编辑 单位要做个小软件,把之前用C编写的程序转化为了二进制BIN文件,可用ULTRAEDIT 打开保存的那种,我想问下可否用VB做一个界面让BIN文件和EXCEL文件相互转换?因为程序里只是些固定地址参数设置,如果一个个在BIN上改的话容易出错,所以希望先将BIN文件转换成EXCEL文件,统一改了参数后再转换成BIN,从而写到对应的芯片中;网上查不到什么资料,有的说用VB.NET调用个文件就可以实现,可是还是感觉不那么容易,急求解!!! BIN??转化?EXCEL?? Excel BIN 转化
[解决办法]
楼主先看下这个。
VBA的代码,你新建一个Excel工作簿,把这段代码粘贴到它的标准模块中。
运行一下,看效果。
Option Explicit
Private Sub LoadTest()
Dim aBuffer() As Byte
Dim strCMD$, i&, j&, r&
strCMD = Replace(Environ$("ComSpec"), "cmd", "notepad", , , 1)
Open strCMD For Binary As #1
aBuffer = InputB(LOF(1), 1)
Close #1
Sheet1.Range("A:A").ColumnWidth = 10
Sheet1.Range("B:Q").ColumnWidth = 3.5
Sheet1.Range("A:Q").NumberFormatLocal = "@"
Sheet1.Range("A:Q").HorizontalAlignment = xlCenter
r = 1
For i = 0 To 15
Sheet1.Cells(r, i + 2) = Hex$(i)
Next
For i = 0 To (UBound(aBuffer) And &H7FFFFFF0) - 1
r = r + 1
Sheet1.Cells(r, 1).Value = Right$("00000000" & Hex$(i), 8)
For j = 0 To 15
Sheet1.Cells(r, j + 2).Value = Right$("0" & Hex$(aBuffer(i + j)), 2)
Next
i = i + 15
Next
End Sub
'窗体上放两个文本框,一个按钮,名称均为默认.
'
'By 嗷嗷叫的老马
'
Option Explicit
Private Sub Command1_Click()
Dim lFn As Long
lFn = OpenBin("c:\xxx.bin") '打开文件
If lFn <> -1 Then
Put #lFn, CLng(Text2.Text), CByte(Text1.Text) 'Text2.Text中是要写入的位置(第1个字节就是位置1),Text1.Text中是要写入的内容,由于是字节,值在0-255间.
Put #lFn, CLng(Text2.Text), CLng(Text1.Text) '写入一个四字节内容的情况.
End If
Call CloseBin(lFn) '关闭文件
End Sub
Private Function OpenBin(ByVal strFileName As String) As Long
'打开一个BIN文件.
'
'返回值是已打开文件的文件号,以便外部操作.
'如果出现错误则返回-1,具体错误原因在Err.Number中.
'
Dim lFn As Long
On Error GoTo errH
lFn = FreeFile
Open strFileName For Binary As #lFn
OpenBin = lFn
Exit Function
errH:
OpenBin = -1
End Function
Private Sub CloseBin(ByVal FileNum As Long)
'关闭一个文件.
'
'输入参数是文件号,如果值是-1,则关闭所有已用Open打开的文件.
'
On Error Resume Next
If FileNum <> -1 Then
Close #FileNum
Else
Close
End If
End Sub
Option Explicit
Private Sub LoadData()
Dim aBuffer() As Byte
Dim strFile$, i&, j&, r&
Dim lFileSize&
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "BIN文件(*.bin)", "*.bin"
.Filters.Add "所有文件(*.*)", "*.*"
If .Show = 0 Then MsgBox "没有选择文件。", vbInformation: Exit Sub
strFile = .SelectedItems(1)
End With
Open strFile For Binary As #1
lFileSize = LOF(1)
aBuffer = InputB(LOF(1), 1)
Close #1
strFilename = strFile
Sheet1.Range("A:Q").Delete
Sheet1.Range("A:A").ColumnWidth = 10
Sheet1.Range("B:Q").ColumnWidth = 3.5
Sheet1.Range("A:Q").NumberFormatLocal = "@"
Sheet1.Range("A:Q").HorizontalAlignment = xlCenter
r = 1
For i = 0 To 15
Sheet1.Cells(r, i + 2) = Hex$(i)
Next
For i = 0 To (lFileSize And &H7FFFFFF0) - 1
r = r + 1
Sheet1.Cells(r, 1).Value = Right$("00000000" & Hex$(i), 8)
For j = 0 To 15
Sheet1.Cells(r, j + 2).Value = Right$("0" & Hex$(aBuffer(i + j)), 2)
Next
i = i + 15
Next
j = lFileSize And 15
If (j > 0) Then
r = r + 1
Sheet1.Cells(r, 1).Value = Right$("00000000" & Hex$(i), 8)
For j = 0 To j - 1
Sheet1.Cells(r, j + 2).Value = Right$("0" & Hex$(aBuffer(i + j)), 2)
Next
End If
End Sub
Private Sub SaveData(strFile As String)
Const BUFFERSIZE As Long = 4096 '每次写4KB数据
Dim aBuffer() As Byte
Dim strTemp$, i&, j&, r&
'清空原始文件(如果编辑后文件长度不会减少,可以不用这个)
Open strFile For Output As #1: Close #1
'写数据
Open strFile For Binary As #1
ReDim aBuffer(BUFFERSIZE - 1)
i = 0: r = 2 '第2行开始
Do
For j = 2 To 17
strTemp = Sheet1.Cells(r, j)
If (Len(strTemp) = 0) Then Exit For
aBuffer(i) = CByte("&H" & strTemp)
i = i + 1
Next
If (i = BUFFERSIZE) Then i = 0: Put #1, , aBuffer
If (Len(strTemp) = 0) Then
If (i = 0) Then Exit Sub
ReDim Preserve aBuffer(i - 1)
Put #1, , aBuffer
Exit Sub
End If
r = r + 1
Loop
End Sub
Private Sub CommandButton1_Click()
Call LoadData
Sheet1.Cells(1, 1).Select
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim lFlag&, strTemp$
If Len(strFilename) > 0 Then
lFlag = MsgBox("保存数据到:" & strFilename, 35)
If (vbCancel = lFlag) Then Exit Sub
lFlag = vbNo = lFlag
Else
lFlag = -1
End If
If (lFlag) Then
strTemp = Application.GetSaveAsFilename( _
filefilter:="BIN文件(*.bin),*.bin,所有文件(*.*),*.*", _
Title:="保存文件")
If (strTemp = "False") Then
MsgBox "没有保存数据!", vbExclamation
Unload Me
Exit Sub
End If
strFilename = strTemp '更换文件名
Else
strTemp = strFilename
End If
Call SaveData(strTemp): Close
Unload Me
End Sub