Skip to content

Instantly share code, notes, and snippets.

@redraiment
Last active October 30, 2025 19:59
Show Gist options
  • Select an option

  • Save redraiment/b22a5962e5ded4ad1ce4da146cf9175b to your computer and use it in GitHub Desktop.

Select an option

Save redraiment/b22a5962e5ded4ad1ce4da146cf9175b to your computer and use it in GitHub Desktop.
DuckDB ODBC UTF-8 Character Encoding Fix for Excel VBA
Function ConvertUtf8ToUnicode(bytes() As Byte) As String
Dim ostream As Object
Set ostream = CreateObject("ADODB.Stream")
With ostream
.Type = 1 ' Binary
.Open
.Write bytes
.Position = 0
.Type = 2 'Text
.Charset = "UTF-8"
ConvertUtf8ToUnicode = .ReadText(-1)
.Close
End With
End Function
Public Sub Execute(sql As String, range As range)
Dim connection As Object
Set connection = CreateObject("ADODB.Connection")
connection.Open "Driver={DuckDB Driver};Database=:memory:;"
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.Open sql, connection
Dim data As Variant
data = rs.GetRows()
Dim rows As Long, cols As Long
cols = UBound(data, 1)
rows = UBound(data, 2)
Dim cells As Variant
ReDim cells(rows, cols)
rs.MoveFirst
Dim row As Long, col As Long, bytes() As Byte
For row = 0 To rows
For col = 0 To cols
If adVarChar <= rs.Fields(col).Type And rs.Fields(col).Type <= adLongVarBinary And Not IsNull(rs.Fields(col).value) Then
bytes = data(col, row)
cells(row, col) = ConvertUtf8ToUnicode(bytes)
Else
cells(row, col) = data(col, row)
End If
Next col
rs.MoveNext
Next row
range.Resize(rows + 1, cols + 1).value = cells
rs.Close
Set rs = Nothing
connection.Close
Set connection = Nothing
End Sub
@redraiment
Copy link
Author

Usage:

DuckDB.Execute "select encode('你好') as Chinese", Range("A1")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment