|
楼主 |
发表于 2016-4-13 20:13
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 山中老人 于 2016-4-13 20:27 编辑
代码 主要是 演示:【文件】的二进制读写,以及与【16进制数字 字符串】间的转换!
生成SQL语句范例:
Dim f As String, Arr() As Byte, T As String
Dim SQLstr As String, RS As New ADODB.Recordset
Dim L As Long
'上传文件
f = "C:\Users\Administrator\Desktop\xl.xls" '源文件
Arr = FileToByteArr(f) '将文件 读到 字节数组中
T = ByteArrTo16Str(Arr) '将字节数组 转换为 16进制数字 字符串
T = Str16_ADD0x(T) '16进制数字 字符串,加头标“0x”
'Debug.Print T
SQLstr = "UPDATE [KSCHTJ].[FileS].[库] SET [IMG]=" & T & " WHERE [ID]=14 " 'SQL语句文本
Debug.Print SQLstr
- Public Function ByteArrToFile(PathFile As String, ByteArr() As Byte) As Boolean '用字节数组 生成 文件
- On Error GoTo err1
- ByteArrToFile = False
- If LBound(ByteArr, 1) < 0 Or UBound(ByteArr, 1) < 0 Then Exit Function '空数组
- Open PathFile For Binary Access Write As #1
- Put #1, , ByteArr
- Close #1
- ByteArrToFile = True
- Exit Function
- err1:
- MsgBox "[ByteArrToFile]错误!" & Chr(13) & "ERR:" & err.Description
- End Function
- Public Function FileToByteArr(PathFile As String) As Byte() '将文件 读到 字节数组中
- On Error GoTo err1
- Dim Arr() As Byte
- ReDim Arr(-1 To -1) '空数组
- FileToByteArr = Arr
- If PathFile = "" Then Exit Function
- If Dir(PathFile) = "" Then Exit Function
- Dim i As Long, L As Long
- L = FileLen(PathFile)
- ReDim Arr(0 To L - 1)
- Open PathFile For Binary Access Read As #1
- Get #1, , Arr
- Close #1
- FileToByteArr = Arr
- Exit Function
- err1:
- MsgBox "[FileToByteArr]错误!" & Chr(13) & "ERR:" & err.Description
- End Function
- Public Function ByteArrTo16Str(ByteArr() As Byte) As String '将字节数组 转换为 16进制数字 字符串
- On Error GoTo err1
- ByteArrTo16Str = ""
- If LBound(ByteArr, 1) < 0 Or UBound(ByteArr, 1) < 0 Then Exit Function
- Dim i As Long, L As Long
- Dim Str As String
- L = UBound(ByteArr, 1)
- Str = ""
- For i = 0 To L
- Str = Str & Right("00" & Hex(ByteArr(i)), 2)
- Next
- ByteArrTo16Str = Str
- Exit Function
- err1:
- MsgBox "[ByteArrTo16Str]错误!" & Chr(13) & "ERR:" & err.Description
- End Function
- Public Function Str16ToByteArr(ByVal Str16 As String) As Byte() '将16进制数字 字符串 转换为 字节数组
- On Error GoTo err1
- Dim H As String, i As Long, L As Long
- Dim B As Byte, Arr() As Byte
- ReDim Arr(-1 To -1) '空数组
- Str16ToByteArr = Arr
- Str16 = Replace(Str16, " ", "")
- If Str16 = "" Then Exit Function
-
- Str16 = Str16_Del0x(Str16) '去除头标“0x”
- If Str16 = "" Then Exit Function
- '长度
- L = Len(Str16) / 2 - 1
- ReDim Arr(0 To L)
- '转换值
- For i = 0 To L
- H = Mid(Str16, i * 2 + 1, 2)
- B = CLng("&H" & H)
- Arr(i) = B
- Next
- Str16ToByteArr = Arr
- Exit Function
- err1:
- MsgBox "[Str16ToByteArr]错误!" & Chr(13) & "ERR:" & err.Description
- End Function
- Public Function Str16_Del0x(ByVal Str16 As String) As String '16进制数字 字符串,去除头标“0x”
- Str16_Del0x = Str16
- If Len(Str16) < 2 Then Exit Function
- Dim H As String
- H = UCase(Left(Str16, 2))
- If H = "0X" Then
- Str16 = Mid(Str16, 3)
- End If
- Str16_Del0x = Str16
- Exit Function
- End Function
- Public Function Str16_ADD0x(ByVal Str16 As String) As String '16进制数字 字符串,加头标“0x”
- Str16_ADD0x = Str16
- If Str16 = "" Then Exit Function
- Dim H As String
- H = UCase(Left(Str16, 2))
- If H = "0X" Then
- Exit Function
- End If
- Str16_ADD0x = "0x" & Str16
- Exit Function
- End Function
复制代码
|
|