外部 UTF-8 ファイル
毎回同じ質問を Google 先生にしているので、いい加減まとめておきます。。。
【環境】
Excel:2010
Excel:2010
準備
文字コード UTF-8 に対応するためには、ライブラリの参照設定が必要。
Visual Basec Editor の
[ツール]-[参照設定]
[参照可能なライブラリ ファイル]
[Microsoft ActiveX Data Object *.* Library]
というのが複数並んでいるので、2.5以降にチェック。
(まぁ、最新がよいのではないでしょうか)
読み込み
文字コード変換
- Dim csv As String
- With CreateObject("ADODB.Stream")
- .Charset = "UTF-8"
- .Open
- .LoadFromFile "C:\hoge.csv"
- csv = .ReadText
- .Close
- End With
これで、変数 csv に、ファイル全体文字列が格納されているので
適宜整形して、利用。
改行コード変換
改行コード については、こんな感じ。
Excel 用に vbCrLf で統一しておく方が問題が出ないので
下記のように置換すると吉。
- csv = Replace(csv, vbLf, vbCrLf)
- csv = Replace(csv, vbCr, vbCrLf)
利用
CSV
- Dim arrLines As Variant
- arrLines = Split(csv, vbCrLf)
- Dim arrFieldsAs Variant
- For index = 0 To UBound(arrLines)
- arrFields= Split(arrLines(index ), ",")
- Next
書き出し
読み込み編とは時期が異なったので、書き方が全然違うけど、
そこは気にしない!
- Dim string as String
- Dim i As Long
- Dim j As Integer
- Dim outStream As ADODB.Stream
- Set outStream = New ADODB.Stream
- 'ADODB.Stream の設定
- With outStream
- .Type = adTypeText
- .Charset = "UTF-8"
- .LineSeparator = adLF '改行コードをLFに指定(adCRLF)
- .Open
- End With
- ' データの書き出し
- For i = 1 To 100
- For j = 1 To 5
- If j > 1 Then string = string & ","
- string = string & Cells(i, j).Value
- Next j
- outStream.WriteText string , adWriteLine
- Next i
- 'BOMを削る前処理
- outStream.Position = 0 'ファイル先頭にセット
- outStream.Type = adTypeBinary 'バイナリに変更
- outStream.Position = 3 'BOMの3バイト分スキップ
- 'BOMを削った分をコピー
- Dim outStreamCopy As New ADODB.Stream
- outStreamCopy.Type = adTypeBinary
- outStreamCopy.Open
- outStream.CopyTo outStreamCopy
- 'CSVファイルに保存
- outStreamCopy.SaveToFile Activebook.Path & "\text_utf8n.csv", adSaveCreateOverWrite
- 'クローズ処理
- outStream.Close
- outStreamCopy.Close
- Set outStream = Nothing
- Set outStreamCopy = Nothing
ユーティリティー
毎回書いていられないので、ユーティリティークラスを作成。
utilUtf8
- Option Explicit
- Public Function inputStream(ByVal filePath As String) As String
- Dim strWhole As String
- With CreateObject("ADODB.Stream")
- .Charset = "UTF-8"
- .Open
- .LoadFromFile filePath
- strWhole = .ReadText
- .Close
- End With
- strWhole = Replace(strWhole, vbLf, vbCrLf)
- strWhole = Replace(strWhole, vbCr, vbCrLf)
- Set inputStream = strWhole
- End Function
- Public Function getOutStream(ByVal outStream As ADODB.Stream) As ADODB.Stream
- 'ADODB.Stream の設定
- With outStream
- .Type = adTypeText
- .Charset = "UTF-8"
- .LineSeparator = adLF '改行コードをLFに指定
- .Open
- End With
- Set getOutStream = outStream
- End Function
- Public Sub outputStream(ByVal outStream As ADODB.Stream, ByVal filePath As String, ByVal noBom As Boolean)
- If noBom Then
- 'BOMを削る前処理
- outStream.Position = 0 'ファイル先頭にセット
- outStream.Type = adTypeBinary 'バイナリに変更
- outStream.Position = 3 'BOMの3バイト分スキップ
- End If
- 'BOMを削った場合に備えてコピーを出力
- Dim outStreamCopy As New ADODB.Stream
- outStreamCopy.Type = adTypeBinary
- outStreamCopy.Open
- outStream.CopyTo outStreamCopy
- 'ファイルに保存
- outStreamCopy.SaveToFile filePath, adSaveCreateOverWrite
- outStreamCopy.Close
- Set outStreamCopy = Nothing
- outStream.Close
- End Sub
参考サイト
Excel VBAでUTF-8(BOM無し)に変換してCSV出力してみる
→書き出し編。参考というより、ソースを頂戴しました。ありがとうございます♪