私は80バイトのラインフォーマットを必要とするレポートを生成するスクリプトを作成しています。
現在、私のスクリプトはすべてのフィールドを正しくフォーマットし、それらを単一の列に連結し、残りを削除します。この連結列には、保存時に削除できないフィールドを区切るスペースがあります。これはWindows 10のExcel 2016の64ビット版で行われています。
ファイルをUTF-8でエンコードされたテキストファイルとして保存するにはどうすればよいですか?UTF-8エンコーディングでTXTにシートを保存
2
A
答えて
0
0
は
UTFTest.bas
' Converting a VBA string to an array of bytes in UTF-8 encoding
' $Date: 2015-06-30 10:05Z $
' $Original Author: David Ireland $
' Copyright (C) 2015 DI Management Services Pty Limited
' <http://www.di-mgt.com.au> <http://www.cryptosys.net>
Option Explicit
Option Base 0
''' Extract a set of VBA "Unicode" strings from Excel sheet, encode in UTF-8 and display details
Public Sub ShowStuff()
Dim strData As String
' Plain ASCII
' "abc123"
' U+0061, U+0062, U+0063, U+0031, U+0032, U+0033
' EXCEL: Get value from cell A1
strData = Worksheets("Sheet1").Cells(1, 1)
Debug.Print vbCrLf & Worksheets("Sheet1").Cells(1, 2)
ProcessString (strData)
' Spanish
' LATIN SMALL LETTER[s] [AEIO] WITH ACUTE and SMALL LETTER N WITH TILDE
' U+00E1, U+00E9, U+00ED, U+00F3, U+00F1
' EXCEL: Get value from cell A3
strData = Worksheets("Sheet1").Cells(3, 1)
Debug.Print vbCrLf & Worksheets("Sheet1").Cells(3, 2)
ProcessString (strData)
' Japanese
' "Hello" in Hiragana characters is KO-N-NI-TI-HA (Kon'nichiwa)
' U+3053 (hiragana letter ko), U+3093 (hiragana letter n),
' U+306B (hiragana letter ni), U+3061 (hiragana letter ti),
' and U+306F (hiragana letter ha)
' EXCEL: Get value from cell A5
strData = Worksheets("Sheet1").Cells(5, 1)
Debug.Print vbCrLf & Worksheets("Sheet1").Cells(5, 2)
ProcessString (strData)
' Chinese
' CN=ben (U+672C), C= zhong guo (U+4E2D, U+570B), OU=zong ju (U+7E3D, U+5C40)
' EXCEL: Get value from cell A7
strData = Worksheets("Sheet1").Cells(7, 1)
Debug.Print vbCrLf & Worksheets("Sheet1").Cells(7, 2)
ProcessString (strData)
' Hebrew
' "abc" U+0061, U+0062, U+0063
' SPACE U+0020
' [NB right-to-left order]
' U+05DB HEBREW LETTER KAF
' U+05E9 HEBREW LETTER SHIN
' U+05E8 HEBREW LETTER RESH
' SPACE "f123" U+0066 U+0031 U+0032 U+0033
' EXCEL: Get value from cell A9
strData = Worksheets("Sheet1").Cells(9, 1)
Debug.Print vbCrLf & Worksheets("Sheet1").Cells(9, 2)
ProcessString (strData)
End Sub
Public Function ProcessString(strData As String)
Dim abData() As Byte
Dim strOutput As String
Debug.Print strData ' This should show "?" for non-ANSI characters
strOutput = Utf8BytesFromString(strData)
abData = strOutput
' Reset array width to Actual Number of Bytes
ReDim Preserve abData(Len(strOutput) - 1)
Debug.Print bv_HexFromBytesSp(abData)
Debug.Print "Strlen=" & Len(strData) & " chars; utf8len=" & Len(strOutput) & " bytes"
End Function
''' Returns hex-encoded string from array of bytes (with spaces)
''' E.g. aBytes(&HFE, &HDC, &H80) will return "FE DC 80"
Public Function bv_HexFromBytesSp(aBytes() As Byte) As String
Dim i As Long
If Not IsArray(aBytes) Then
Exit Function
End If
For i = LBound(aBytes) To UBound(aBytes)
If (i > 0) Then bv_HexFromBytesSp = bv_HexFromBytesSp & " "
If aBytes(i) < 16 Then
bv_HexFromBytesSp = bv_HexFromBytesSp & "0" & Hex(aBytes(i))
Else
bv_HexFromBytesSp = bv_HexFromBytesSp & Hex(aBytes(i))
End If
Next
End Function
そして、Win64のはhttp://www.di-mgt.com.au/howto-convert-vba-unicode-to-utf8.html
から抽出したようAPIが' basUtf8FromString
' Written by David Ireland DI Management Services Pty Limited 2015
' <http://www.di-mgt.com.au> <http://www.cryptosys.net>
Option Explicit
' CodePage constant for UTF-8
Private Const CP_UTF8 = 65001
#If Win64 Then
Private Declare PtrSafe Function GetACP Lib "Kernel32"() As LongPtr
Private Declare PtrSafe Function MultiByteToWideChar Lib "Kernel32" (ByVal CodePage As LongPtr, _
ByVal dwflags As LongPtr, ByVal lpMultiByteStr As LongPtr, ByVal cchMultiByte As LongPtr, _
ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As LongPtr) As LongPtr
Private Declare PtrSafe Function WideCharToMultiByte Lib "Kernel32" (ByVal CodePage As LongPtr, _
ByVal dwflags As LongPtr, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As LongPtr, _
ByVal lpMultiByteStr As LongPtr, ByVal cchMultiByte As LongPtr, ByVal lpDefaultChar As LongPtr, _
lpUsedDefaultChar As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function GetACP Lib "Kernel32"() As Long
Private Declare PtrSafe Function MultiByteToWideChar Lib "Kernel32" (ByVal CodePage As Long, _
ByVal dwflags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare PtrSafe Function WideCharToMultiByte Lib "Kernel32" (ByVal CodePage As Long, _
ByVal dwflags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, _
lpUsedDefaultChar As Long) As Long
#End If
''' Return byte array with VBA "Unicode" string encoded in UTF-8
Public Function Utf8BytesFromString(strInput As String) As String
Dim nBytes As LongPtr
Dim pwz As LongPtr
Dim pwzBuffer As LongPtr
Dim sBuffer As String
' Get length in bytes *including* terminating null
pwz = StrPtr(strInput)
nBytes = WideCharToMultiByte(CP_UTF8, 0&, pwz, -1, 0&, 0&, ByVal 0&, ByVal 0&)
sBuffer = String$(nBytes + 1, vbNullChar)
pwzBuffer = StrPtr(sBuffer)
nBytes = WideCharToMultiByte(CP_UTF8, 0&, pwz, -1, pwzBuffer, Len(sBuffer), ByVal 0&, ByVal 0&)
Utf8BytesFromString = Left$(sBuffer, nBytes - 1)
End Function
を呼び出して変換Officeの64ビット用に変更、そのサンプルスプレッドシートからのコードです
関連する問題
- 1. Perlエンコーディング - ファイルをUTF8に保存
- 2. DOS737からUTF8へのtxtファイルのエンコーディングを変換する
- 3. Java UTF8エンコーディング
- 4. C++ UTF8エンコーディング
- 5. osx:newbie様々なエンコーディングの5k txtファイルをutf8でエンコードされたtxtファイルに変換する
- 6. プログラムで "PDFに保存..."シート
- 7. MemoryStream UTF8エンコーディングのStreamReader
- 8. Python UTF16LEファイルからUTF8エンコーディング
- 9. PHP、PDO、SLIMフレームワーク、JSONエンコーディングUTF8
- 10. Python未知のエンコーディングutf8
- 11. UTF8文字エンコーディング、htmlentities、クロスサイトスクリプティング
- 12. RubyのCSV UTF8エンコーディング・エラー
- 13. ios UTF8エンコーディングからnsstring
- 14. Ruby UTF8エンコーディングの問題
- 15. Rails UTF8エンコーディングの問題
- 16. Doctrineデータベースにデータを保存するときにUTF8エンコーディングをhtmlエンティティに変更する
- 17. ObservableCollectionをファイル(.txt)に保存する
- 18. apache pig、結果をtxtファイルに保存
- 19. numpy配列をtxtに保存
- 20. ファイルリストを.txtに保存する
- 21. リストをtxtファイルに保存する
- 22. 配列をtxtファイルに保存
- 23. データをtxtに保存する
- 24. 印刷出力を.txtに保存
- 25. 既存のExcelシートを保存する
- 26. シートの保存でアクションを開始
- 27. GoogleフォームをExcelシートに保存
- 28. 他のシートに列を保存する
- 29. RailsプロジェクトでMysqlエンコーディングutf8をutf8mb4に変換するには
- 30. Emacsを開いて保存するエンコーディング
"ScriptUtils.ByteArray"が参照リストにあることを確認してください。Tools - > ReferencesでScriptUtils.ByteArray – HA560
@ HA560を検索してください。私は[ここ](http://www.motobit.com/help/scptutl/cm119.htm)からスクリプトを入手し、私は自分の[ScriptUtilities](http://www.motobit。 com/help/scptutl/default.htm)ソフトウェアをインストールします。 編集:たわごと。それは費用がかかる。 – drdb
購入したユーティリティなしで、vbaでバイト配列を作成できます。 StrConv、vbUnicode、Byte型の配列を使用した例がたくさんあります。 – dbmitch