Access VBA 使用API 讀寫 UTF-8 文本文件的內(nèi)容
- 2017-07-26 17:42:00
- zstmtony 轉(zhuǎn)貼
- 4353
Access VBA 使用API 讀寫 UTF-8 文本文件的內(nèi)容
這是一箇轉(zhuǎn)換UTF-8格式文本文件的示例,包括讀取和寫入,需要用到兩箇API函數(shù):MultiByteToWideChar和WideCharToMultiByte Public Declare Function MultiByteToWideChar Lib "kernel32" ( _ ByVal CodePage As Long, _ ByVal dwFlags As Long, _ ByRef lpMultiByteStr As Any, _ ByVal cchMultiByte As Long, _ ByVal lpWideCharStr As Long, _ ByVal cchWideChar As Long) As Long Public Declare Function WideCharToMultiByte Lib "kernel32" ( _ ByVal CodePage As Long, _ ByVal dwFlags As Long, _ ByVal lpWideCharStr As Long, _ ByVal cchWideChar As Long, _ ByRef lpMultiByteStr As Any, _ ByVal cchMultiByte As Long, _ ByVal lpDefaultChar As String, _ ByVal lpUsedDefaultChar As Long) As Long Public Const CP_UTF8 = 65001 ' 將輸入文本寫進(jìn)UTF8格式的文本文件 ' 輸入 ' strInput:文本字符串 ' strFile:保存的UTF8格式文件路徑 ' bBOM:True錶示文件帶"EFBBBF"頭,F(xiàn)alse錶示不帶 Sub WriteUTF8File(strInput As String, strFile As String, Optional bBOM As Boolean = True) Dim bByte As Byte Dim ReturnByte() As Byte Dim lngBufferSize As Long Dim lngResult As Long Dim TLen As Long ' 判斷輸入字符串是否爲(wèi)空 If Len(strInput) = 0 Then Exit Sub On Error GoTo errHandle ' 判斷文件是否存在,如存在則刪除 If Dir(strFile) <> "" Then Kill strFile TLen = Len(strInput) lngBufferSize = TLen * 3 + 1 ReDim ReturnByte(lngBufferSize - 1) lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strInput), TLen, _ ReturnByte(0), lngBufferSize, vbNullString, 0) If lngResult Then lngResult = lngResult - 1 ReDim Preserve ReturnByte(lngResult) Open strFile For Binary As #1 If bBOM = True Then bByte = 239 Put #1, , bByte bByte = 187 Put #1, , bByte bByte = 191 Put #1, , bByte End If Put #1, , ReturnByte Close #1 End If Exit Sub errHandle: MsgBox Err.Description, , "錯(cuò)誤 - " & Err.Number End Sub ' 讀取UTF8文件併轉(zhuǎn)換爲(wèi)VBA中可讀的字符串 ' 輸入 ' strFile:UTF8格式文件的路徑 Function readUTF8File(strFile As String) As String Dim bByte As Byte Dim ReturnByte() As Byte Dim lngBufferSize As Long Dim strBuffer As String Dim lngResult As Long Dim bHeader(1 To 3) As Byte Dim i As Long On Error GoTo errHandle If Dir(strFile) = "" Then Exit Function ' 以二進(jìn)製打開文件 Open strFile For Binary As #1 ReDim ReturnByte(0 To LOF(1) - 1) As Byte ' 讀取前三箇字節(jié) Get #1, , bHeader(1) Get #1, , bHeader(2) Get #1, , bHeader(3) ' 判斷前三箇字節(jié)是否爲(wèi)B(tài)OM頭 If bHeader(1) = 239 And bHeader(2) = 187 And bHeader(3) = 191 Then For i = 3 To LOF(1) - 1 Get #1, , ReturnByte(i - 3) Next i Else ReturnByte(0) = bHeader(1) ReturnByte(1) = bHeader(2) ReturnByte(2) = bHeader(3) For i = 3 To LOF(1) - 1 Get #1, , ReturnByte(i) Next i End If ' 關(guān)閉文件 Close #1 ' 轉(zhuǎn)換UTF-8數(shù)組爲(wèi)字符串 lngBufferSize = UBound(ReturnByte) + 1 strBuffer = String$(lngBufferSize, vbNullChar) lngResult = MultiByteToWideChar(65001, 0, ReturnByte(0), _ lngBufferSize, StrPtr(strBuffer), lngBufferSize) readUTF8File = Left(strBuffer, lngResult) Exit Function errHandle: MsgBox Err.Description, , "錯(cuò)誤 - " & Err.Number readUTF8File = "" End Function ' 讀取UTF8文件測試 Sub readFileTest() Dim strFile As String Dim strContent As String Dim strSaveFile As String ' 穫?cè)∥募吐窂? strFile = Application.GetOpenFilename("文本文件,*.txt", , "打開文本文件") If strFile = "False" Then Exit Sub strContent = readUTF8File(strFile) If MsgBox("是否需要保存轉(zhuǎn)換好的ANSI文本?", vbYesNo, "保存") = vbYes Then strSaveFile = Application.GetSaveAsFilename(Mid(strFile, InStrRev(strFile, "/") + 1), "文本文件,*.txt") If strSaveFile = "False" Then Exit Sub Open strSaveFile For Binary As #1 Put #1, , strContent Close #1 End If End Sub ' 寫入U(xiǎn)TF8文件測試 Sub writeFileTest() Dim strFile As String Dim strContent As String strContent = "這是一箇UTF8文檔測試" strFile = Application.GetSaveAsFilename("", "文本文件,*.txt") If strFile = "False" Then Exit Sub 'WriteUTF8File strContent, strFile WriteUTF8File strContent, strFile, False End Sub
分享
Access數(shù)據(jù)庫自身
- office課程播放地址及課程明細(xì)
- Excel Word PPT Access VBA等Office技巧學(xué)習(xí)平颱
- 將( .accdb) 文件格式數(shù)據(jù)庫轉(zhuǎn)換爲(wèi)早期版本(.mdb)的文件格式
- 將早期的數(shù)據(jù)庫文件格式(.mdb)轉(zhuǎn)換爲(wèi) (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 數(shù)據(jù)庫引擎和訪問連接引擎以阻止對(duì)遠(yuǎn)程數(shù)據(jù)庫的訪問(remote table)
- Access 365 /Access 2019 數(shù)據(jù)庫中哪些函數(shù)功能和屬性被沙箱模式阻止(如未啟動(dòng)宏時(shí))
- Access Runtime(運(yùn)行時(shí))最全的下載(2007 2010 2013 2016 2019 Access 365)
Access VBA函數(shù)模塊
Access Activex第三方控件
- Activex控件或Dll 在某些電腦無法正常註冊的解決辦法(regsvr32註冊時(shí)卡?。?/a>
- office使用部分控件時(shí)提示“您沒有使用該ActiveX控件許可的問題”的解決方法
- RTF文件(富文本格式)的一些解析
- Access樹控件(treeview) 64位Office下齣現(xiàn)橫曏滾動(dòng)條不會(huì)自動(dòng)定位的解決辦法
- Access中國樹控件 在win10電腦 節(jié)點(diǎn)行間距太小的解決辦法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 樹控件 ListView列錶等64位MSCOMMCTL.OCX控件下載
- VBA或VB6調(diào)用WebService(直接Post方式)併解析返迴的XML
Access ADP Sql Server等
- 早期PB程序連接Sqlserver齣現(xiàn)錯(cuò)誤
- MMC 不能打開文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由於文件不存在,不是一箇MMC控製颱,或者用後來的MMC版
- sql server連接不瞭的解決辦法
- localhost與127.0.0.1區(qū)彆
- Roych的淺談數(shù)據(jù)庫開髮繫列(Sql Server)
- sqlserver 自動(dòng)備份對(duì)備份目録沒有存取權(quán)限的解決辦法
- 安裝Sql server 2005 express 和SQLServer2005 Express版企業(yè)管理器 SQLServer2005_SSMSEE
文章分類
聯(lián)繫我們
聯(lián)繫人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |