0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

csvを開いた時に、エンコードを確認する方法

Posted at
Sub DetectEncoding()
    Dim filePath As String
    Dim fileNum As Integer
    Dim bom(0 To 2) As Byte
    Dim encoding As String
    
    filePath = "C:\path\to\your\file.csv"
    
    ' ファイルをバイナリモードで開く
    fileNum = FreeFile
    Open filePath For Binary As #fileNum
    
    ' ファイルの最初の3バイトを読み取る
    Get #fileNum, , bom
    Close #fileNum
    
    ' バイトオーダーマークを確認してエンコーディングを判別
    If bom(0) = &HEF And bom(1) = &HBB And bom(2) = &HBF Then
        encoding = "UTF-8"
    ElseIf bom(0) = &HFF And bom(1) = &HFE Then
        encoding = "UTF-16 (LE)"
    ElseIf bom(0) = &HFE And bom(1) = &HFF Then
        encoding = "UTF-16 (BE)"
    Else
        encoding = "SJIS (またはその他)"
    End If
    
    MsgBox "このファイルのエンコーディングは " & encoding & " です。"
End Sub

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?