Access VBA 使用API 讀寫 UTF-8 文本文件的內(nèi)容
- 2017-07-26 17:42:00
- zstmtony 轉(zhuǎn)貼
- 4352
Access VBA 使用API 讀寫 UTF-8 文本文件的內(nèi)容
這是一個(gè)轉(zhuǎn)換UTF-8格式文本文件的示例,包括讀取和寫入,需要用到兩個(gè)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 ' 判斷輸入字符串是否為空 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)換為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 ' 讀取前三個(gè)字節(jié) Get #1, , bHeader(1) Get #1, , bHeader(2) Get #1, , bHeader(3) ' 判斷前三個(gè)字節(jié)是否為BOM頭 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ù)組為字符串 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文件測(cè)試 Sub readFileTest() Dim strFile As String Dim strContent As String Dim strSaveFile As String ' 獲取文件名和路徑 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文件測(cè)試 Sub writeFileTest() Dim strFile As String Dim strContent As String strContent = "這是一個(gè)UTF8文檔測(cè)試" 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í)平臺(tái)
- 將( .accdb) 文件格式數(shù)據(jù)庫轉(zhuǎn)換為早期版本(.mdb)的文件格式
- 將早期的數(shù)據(jù)庫文件格式(.mdb)轉(zhuǎn)換為 (.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 vba代碼太長(zhǎng),換行,分行的寫法
- VB6 VBA Access真正可用并且完美支持中英文的 URLEncode 與 URLDecode 函數(shù)源碼
- 自定義VB中的urlencode函數(shù),將URL中特殊部分進(jìn)行編碼
- Access 函數(shù)簡(jiǎn)化串接sql字符串,減少符號(hào)導(dǎo)致的書寫錯(cuò)誤
- vba完全關(guān)閉IE瀏覽器及調(diào)用IE瀏覽器的簡(jiǎn)單應(yīng)用
- 利用FollowHyperlink方法打開超鏈接提示“無法下載您要求的信息”的解決方案
- 在access中用代碼打開文本框中超鏈接地址
Access Activex第三方控件
- Activex控件或Dll 在某些電腦無法正常注冊(cè)的解決辦法(regsvr32注冊(cè)時(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 可能是由于文件不存在,不是一個(gè)MMC控制臺(tái),或者用后來的MMC版
- sql server連接不了的解決辦法
- localhost與127.0.0.1區(qū)別
- Roych的淺談數(shù)據(jù)庫開發(fā)系列(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 |