'the introduction of two functions, used to solve the utf8 file read and write
Public Declare PtrSafe 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 PtrSafe 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
'the input text written in UTF8 format text file
Input
''strInput: a text string
'strFile: save UTF8 format file path
'bBOM: True indicates file with "EFBBBF head," said False without
Sub WriteUTF8File (strInput As String, strFile As String, Optional bBOM As Boolean=False)
Dim bByte As Byte
Dim ReturnByte () As Byte
Dim lngBufferSize As Long
Dim lngResult As Long
Dim TLen As Long
'whether the input string is empty
If Len (strInput)=0 Then the Exit Sub
On Error GoTo errHandle
'to determine whether a file exists, if there is deleted
If Dir (strFile) & lt;> "" Then Kill strFile
TLen=Len (strInput)
LngBufferSize=TLen * 3 + 1
ReDim ReturnByte (lngBufferSize - 1)
LngResult=WideCharToMultiByte (CP_UTF8, 0, CStr (StrPtr (strInput)), TLen, _
ReturnByte (0), lngBufferSize vbNullString, 0)
If lngResult Then
LngResult=lngResult - 1
ReDim Preserve ReturnByte (lngResult)
The Open strFile For Binary As # 1
If bBOM=True Then
BByte=239
Put # 1, and bByte
BByte=187
Put # 1, and bByte
BByte=191
Put # 1, and bByte
End the If
Put # 1, and ReturnByte
Close # 1
End the If
The Exit Sub
ErrHandle:
MsgBox Err. The Description, "error -" & amp; Err. Number
End Sub
As String Function StrReplace (s, p As String, r As String) As String
Dim re
The Set re=CreateObject (" VBScript. RegExp ")
Re the IgnoreCase=True
Re. Global=True
Re. The Pattern=p
StrReplace=re. Replace (s, r)
End the Function
The Function bTest (ByVal s As String, ByVal p As String) As Boolean
Dim re
The Set re=CreateObject (" VBScript. RegExp ")
Re. The IgnoreCase=False 'set match case
Re. The Pattern=p
BTest=re. The Test (s)
End the Function
Sub achievement_lua ()
Dim the currentPath As String
Dim fileName1 As String
Dim strContent As String
Dim STR As String
Dim str1 As String
Dim splitStr As String
Dim lineArray () As String
Dim lineStr As the Variant
Dim valueArray () As String
Dim tmpStr As String
Dim I, j, x, y, z, maxRow, maxRow2 As Integer
Dim pSheet As Worksheet
Dim taskId As String
Dim dict
The Set dict=CreateObject (" Scripting. The Dictionary ")
Dim dict2
The Set dict2=CreateObject (" Scripting. The Dictionary ")
SplitStr=CRH (10)
CurrentPath=Application. ActiveWorkbook. Path + ""
FileName1=currentPath + "achievement. Lua
"
'==================================================success=========================================
The Set pSheet=ActiveWorkbook. Worksheets (" success ")
'the pSheet. UsedRange. Select
MaxRow=pSheet. UsedRange. Rows. Count
'MsgBox maxRow
STR=""
I=0
For I=2 To maxRow
STR=STR & amp; "{" & amp; SplitStr
If CStr (pSheet Cells (I, 1). The Value)="" Then
The Else
For j=1 To 21
If CStr (pSheet Cells (1, j) Value)="" Then
The Else
Key=CStr (pSheet. Cells (1, j))
Value=https://bbs.csdn.net/topics/CStr (pSheet Cells (I, j))
If (j=18 Or 19 Or j=j=21) Then
STR=STR & amp; "" & amp; The Key & amp; "=" & amp; SplitStr & amp; The Value & amp; SplitStr
The Else
STR=STR & amp; "" & amp; The Key & amp; "=" & amp; The Value & amp; SplitStr
End the If
End the If
Next j
End the If
STR=STR & amp; "} "& amp; SplitStr & amp; SplitStr
Next I
StrContent=strContent & amp; STR
WriteUTF8File strContent, fileName1, False
MsgBox "has successfully exported achievement. The lua data to" & amp; FileName1
End Sub
That lack of sub or function, could you tell me where I went wrong?
CodePudding user response:
Two API statement there, remove the PtrSafe,Written order sample:
Public Declare Function MultiByteToWideChar Lib...
Public Declare Function WideCharToMultiByte Lib...