VB 建UTF-8的文本文件
在VB中怎么建一个UTF-8格式的TXT文件?保存的内容可以记录。比如:我保存了一条记录后再写另一条记录时也同样能保存进去,而且之前的记录不清除。如:第一条:aaaaaa...
在VB中怎么建一个UTF-8格式的TXT文件?
保存的内容可以记录。比如:我保存了一条记录后再写另一条记录时也同样能保存进去,而且之前的记录不清除。
如:第一条:aaaaaaaaa 第二条:bbbbbbbbbb 第三条:ccccccccccc 每条记录都跳行。
最好供应代码。谢谢 展开
保存的内容可以记录。比如:我保存了一条记录后再写另一条记录时也同样能保存进去,而且之前的记录不清除。
如:第一条:aaaaaaaaa 第二条:bbbbbbbbbb 第三条:ccccccccccc 每条记录都跳行。
最好供应代码。谢谢 展开
2个回答
2015-07-10 · 知道合伙人互联网行家
关注
展开全部
VB编程下用UTF-8编码编写的文本文件,都会产生一个BOM,这使得转换出来的文件,在系统编译中编译不过去,必须要把BOM去掉,通过代码实现如下:
Dim app As Excel.Application
Dim eworkbook As Workbook
Dim eworksheet As Worksheet
Dim eworksheet_count As Integer
Dim sheetName As String
Dim obj As Object
Dim FileNum
Dim file_path as String
Dim j as Integer
Dim filepath_save as String
filepath_save = "D:/"
Set app = New Excel.Application //连接EXCEL
Set eworkbook = app.Workbooks.Open(file_path)
eworkbook_count = eworkbook.Worksheets.count
For j = 1 To eworkbook_count
filepath_path = filepath_save & j & ".txt"
Set eworksheet = eworkbook.Sheets(j)
sheetName = eworksheet.Name
Set obj = New ADODB.Stream //设置ADODB流
With obj
.Open
.Charset = "UTF-8"
.Position = .Size
.WriteText "helloworld", 1
.SaveToFile filepath_save
.Close
End With
Set obj = Nothing
Open filepath_save For Input As #1 //消除UTF-8的BOM
Line Input #1, str
mm = Replace(str, str, "msgid """"")
Close #1
Open filepath_save For Binary As #FileNum
Put #FileNum, , mm
Close #FileNum
Next j
Set eworksheet = Nothing
eworkbook.Close
Set eworkbook = Nothing
app.Quit
Set app = Nothing
展开全部
'所用到的API引入
Private 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
' 将输入文本写进UTF8格式的文本文件
' 输入
' strInput:文本字符串
' strFile:保存的UTF8格式文件路径
' bBOM:True表示文件带"EFBBBF"头,False表示不带
Sub WriteUTF8File(strInput As String, strFile As String, Optional bBOM As Boolean = True)
Dim CP_UTF8 As String
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
CP_UTF8 = 65001
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, , "错误 - " & Err.Number
End Sub
Private 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
' 将输入文本写进UTF8格式的文本文件
' 输入
' strInput:文本字符串
' strFile:保存的UTF8格式文件路径
' bBOM:True表示文件带"EFBBBF"头,False表示不带
Sub WriteUTF8File(strInput As String, strFile As String, Optional bBOM As Boolean = True)
Dim CP_UTF8 As String
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
CP_UTF8 = 65001
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, , "错误 - " & Err.Number
End Sub
来自:求助得到的回答
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询