Access导出csv文本文件时自动为所有数据内容加上引号并转为UTF-8格式
- 2017-07-26 18:15:00
- zstmtony 原创
- 6091
最近有个客户需要将他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
- office课程播放地址及课程明细
- Excel Word PPT Access VBA等Office技巧学习平台
- 将( .accdb) 文件格式数据库转换为早期版本(.mdb)的文件格式
- 将早期的数据库文件格式(.mdb)转换为 (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 数据库引擎和访问连接引擎以阻止对远程数据库的访问(remote table)
- Access 365 /Access 2019 数据库中哪些函数功能和属性被沙箱模式阻止(如未启动宏时)
- Access Runtime(运行时)最全的下载(2007 2010 2013 2016 2019 Access 365)
- Activex控件或Dll 在某些电脑无法正常注册的解决办法(regsvr32注册时卡住)
- office使用部分控件时提示“您没有使用该ActiveX控件许可的问题”的解决方法
- RTF文件(富文本格式)的一些解析
- Access树控件(treeview) 64位Office下出现横向滚动条不会自动定位的解决办法
- Access中国树控件 在win10电脑 节点行间距太小的解决办法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 树控件 ListView列表等64位MSCOMMCTL.OCX控件下载
- VBA或VB6调用WebService(直接Post方式)并解析返回的XML
- 早期PB程序连接Sqlserver出现错误
- MMC 不能打开文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由于文件不存在,不是一个MMC控制台,或者用后来的MMC版
- sql server连接不了的解决办法
- localhost与127.0.0.1区别
- Roych的浅谈数据库开发系列(Sql Server)
- sqlserver 自动备份对备份目录没有存取权限的解决办法
- 安装Sql server 2005 express 和SQLServer2005 Express版企业管理器 SQLServer2005_SSMSEE
联系人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |