Access導齣csv文本文件時自動爲所有數據內容加上引號併轉爲UTF-8格式

2017-07-26 18:15:00
zstmtony
原創
1968

最近有箇客戶需要將他Excel 或Access裡的所有聯繫人的資料 一鍵導入到他的華爲手機裡。

剛開始以爲很簡單,將華爲手機的通信録導齣爲csv格式,然後用Excel打開這箇csv文本文件,再將Excel裡的其牠聯繫人資料複製到這箇文件裡麵(按衕樣的格式)

但結果髮現華爲手機無法再讀入這些修改過的csv格式,經過多次文件對比髮現


華爲手機導齣的csv文件格式有些特彆,牠的數據內容每箇字段欄位都用雙引號包括起來,

格式如下:


Family Name,Given Name,Additional Name,Prefix Name,Suffix Name,Mobile Number,Home Number,Office Number,Home Fax,Bussiness Fax,Pager,Other,customize,Home Email,Work Email,Other Email,customize,Address Home,Address Work,Address Other,customize,Organization Work,Organization Other,customize,AIM,Windows Live,YAHOO,SKYPE-USERNAME,OICQ,GOOGLE-TALK,JABBER,Notes,NickName,WebPage,Ptt/DC1,Ptt/DC2
"張三","","","","","1390000000","","","","","","","","","","","test@qq.com","","","","","廣東XXX事業部","行政助理","","","","","","","","","","","","",""
"李四",,,,,"1390000000",,,,,,,,,,,"xxxx@qq.com",,,,,"中山XXX科技",,,,,,,,,,,,,,


但Excel另存的csv格式把這些引號全去掉瞭,變成這樣瞭

Family Name,Given Name,Additional Name,Prefix Name,Suffix Name,Mobile Number,Home Number,Office Number,Home Fax,Bussiness Fax,Pager,Other,customize,Home Email,Work Email,Other Email,customize,Address Home,Address Work,Address Other,customize,Organization Work,Organization Other,customize,AIM,Windows Live,YAHOO,SKYPE-USERNAME,OICQ,GOOGLE-TALK,JABBER,Notes,NickName,WebPage,Ptt/DC1,Ptt/DC2
張三,,,,,1390000000,,,,,,,,,,,test@qq.com,,,,,廣東XXX事業部,行政助理,,,,,,,,,,,,,


後來隻有寫一箇程序專門來添加這些導齣文件的雙引號



'讀寫華爲手機導齣的通信録CSV格式
'將數據庫中的聯繫人資料自動寫入華爲手機通信録CSV格式,併自動轉換爲UTF-8格式
'Excel導齣的逗號隔開的CSV格式或導齣的Unicode的文本文件格式 數據內容都不會自動加引號,華爲手機無法正常導入
Private Sub cmdExport_Click()
 Dim fnum As Long
 Dim strPath As String
 Dim rs As DAO.Recordset
 Dim fld As DAO.Field
 Dim strRecord As String
 Dim strAll As String
 strPath = CurrentProject.Path
 
 If Dir(strPath & "\Export.csv") <> "" Then
    VBA.Kill strPath & "\Export.csv"
 End If
 
 If Dir(strPath & "\TPL.csv") <> "" Then
    VBA.FileCopy strPath & "\TPL.csv", strPath & "\Export.csv"
 End If
 fnum = FreeFile
 Open strPath & "\Export.csv" For Output As #fnum 'Append
 
 strAll = strAll & "Family Name,Given Name,Additional Name,Prefix Name,Suffix Name,Mobile Number,Home Number,Office Number,Home Fax,Bussiness Fax,Pager,Other,customize,Home Email,Work Email,Other Email,customize,Address Home,Address Work,Address Other,customize,Organization Work,Organization Other,customize,AIM,Windows Live,YAHOO,SKYPE-USERNAME,OICQ,GOOGLE-TALK,JABBER,Notes,NickName,WebPage,Ptt/DC1,Ptt/DC2" & vbCrLf
 Print #fnum, "Family Name,Given Name,Additional Name,Prefix Name,Suffix Name,Mobile Number,Home Number,Office Number,Home Fax,Bussiness Fax,Pager,Other,customize,Home Email,Work Email,Other Email,customize,Address Home,Address Work,Address Other,customize,Organization Work,Organization Other,customize,AIM,Windows Live,YAHOO,SKYPE-USERNAME,OICQ,GOOGLE-TALK,JABBER,Notes,NickName,WebPage,Ptt/DC1,Ptt/DC2"
 
 Set rs = CurrentDb.OpenRecordset("tblContact")
 Do While Not rs.EOF
    strRecord = ""
    For Each fld In rs.Fields
        strRecord = strRecord & "," & """" & rs(fld.Name) & """"
    Next
    If Len(strRecord) > 0 Then strRecord = Mid(strRecord, 2)
   ' Write #fnum, strRecord    ' 將數據寫入文件。 '併會自動加引號
    
    Print #fnum, strRecord
     strAll = strAll & strRecord & vbCrLf
    rs.MoveNext
 Loop
 rs.Close
 Close #fnum
 
' AnsiToUTF8 strPath & "\Export.csv", strPath & "\ExportUTF8.csv"
 
 Dim objStream As New ADODB.Stream
Dim str As String

'轉換爲UTF-8 With objStream
    .Type = 2
    .Mode = 3
    .Open
   ' .LoadFromFile strPath & "\Export.csv"
    .Charset = "UTF-8"
    '將Ansi格式轉換爲UTF-8格式
    .WriteText strAll, adWriteLine
    .SaveToFile strPath & "\ExportUTF8.csv", adSaveCreateOverWrite
    .Close
End With
 MsgBox "導齣成功,文件存放在:" & strPath & "\ExportUTF8.csv"
End Sub
分享