Excel VBA将CSV文件保存为UTF8格式

时间:2018-06-29 13:37:01

标签: excel-vba vba excel

Sub LoadData(WkSht As Worksheet, DataSourceName As String, MySQLTable As String, Append As String)

Dim CSVFile As String

WkSht.Cells.Replace ",", ""

CSVFile = "\\ant.domain.com\dept\Extracts\" & DataSourceName & ".csv"

'Save file to desktop.
WkSht.Select
ThisWorkbook.SaveAs Filename:=CSVFile, FileFormat:=xlCSVUTF8

Conn.Open "DSN=cobra2;"

'Delete old data if this table is to be wiped every time.
If Append <> "Append" Then
    Conn.Execute "TRUNCATE TABLE " & MySQLTable
End If

'Load new data in csv at high speed.
Conn.Execute "LOAD DATA LOCAL INFILE '" & Replace(CSVFile, "\", "\\") & "' " & _
             "INTO TABLE " & MySQLTable & " " & _
             "FIELDS TERMINATED BY ',' " & _
             "LINES TERMINATED BY '\r\n' " & _
             "IGNORE 1 LINES"

Set Conn = Nothing

End Sub

当试图使用FileFormat:= xlCSV将conn执行到MySQL 5.7实例时,以上代码通常可以正常工作,给我一个UTF8字符错误。我尝试了以上更改以确保符合UTF8编码,但奇怪的是我们找不到任何可替代的错误。

代码可能是什么问题?

1 个答案:

答案 0 :(得分:1)

FileFormat:= xlCSVUTF8

当前在Excel 2016独立版本中不可用。它仅在内部版本16.0.7726附近的Excel的O365版本中可用