vba判断文件编码格式_vba中读取utf-8格式的csv文件保证不乱码

该博客介绍了如何使用VBA读取UTF-8格式的CSV文件,确保在Excel中导入时不出现乱码。通过设置TextFilePlatform参数为932以匹配Shift-JIS编码,并提供了一个宏示例来读取和处理CSV数据。
摘要由CSDN通过智能技术生成

EXCEL中的导入data的功能。(data-> import   external   data)

Sub   Macro1() '

'   Macro1   Macro

'   test   for   sunhaifeng

'   sunhf@cn.ibm.com

'

If   FileExists( "c:\BookforTestData.csv ")   Then

Kill   "c:\BookforTestData.csv "

End   If

Workbooks.Add

With   ActiveSheet.QueryTables.Add(Connection:= "TEXT;E:\test.csv ",   Destination   _

:=Range( "A1 "))

.Name   =   "test "

.FieldNames   =   True

.RowNumbers   =   False

.FillAdjacentFormulas   =   False

.PreserveFormatting   =   True

.RefreshOnFileOpen   =   False

.RefreshStyle   =   xlInsertDeleteCells

.SavePassword   =   False

.SaveData   =   True

.AdjustColumnWidth   =   True

.RefreshPeriod   =   0

.TextFilePromptOnRefresh   =   False

.TextFilePlatform   =   932

'这里我的cvs文件的编码是shift-jis.需要其他编码的代号可以自己录

.TextFileStartRow   =   1

.TextFileParseType   =   xlDelimited

.TextFileTextQualifier   =   xlTextQualifierDoubleQuote

.TextFileConsecutiveDelimiter   =   False

.TextFileTabDelimiter   =   True

.TextFileSemicolonDelimiter   =   False

.TextFileCommaDelimiter   =   True

.TextFileSpaceDelimiter   =   False

.TextFileColumnDataTypes   =   Array(1,   1,   1,   1)

.TextFileTrailingMinusNumbers   =   True

.Refresh   BackgroundQuery:=False

End   With

ActiveWorkbook.SaveAs   Filename:= "c:\BookforTestData.csv ",   FileFormat:=xlCSV,   _

CreateBackup:=False

ActiveWorkbook.Close   SaveChanges:=True

Dim   MyChar

Dim   strText   As   String

Dim   FileNum   As   Integer

Dim   str()   As   String

Dim   count   As   Integer

Dim   count1   As   Integer

count   =   1

Open   "c:\BookforTestData.csv "   For   Input   As   #1

Do   While   Not   EOF(1)

Line   Input   #1,   strText

str   =   Split(strText,   ", ")

count1   =   UBound(str)

For   i   =   0   To   count1

Worksheets(1).Cells(i   +   1,   count).Value   =   str(i)

Next   i

count   =   count   +   1

Loop

Close   #1

If   FileExists( "c:\BookforTestData.csv ")   Then

Kill   "c:\BookforTestData.csv "

End   If

End   Sub

Private   Function   FileExists(fname)   As   Boolean

Dim   x   As   String

x   =   Dir(fname)

If   x   <>   " "   Then   FileExists   =   True   _

Else   FileExists   =   False

End   Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值