zoukankan      html  css  js  c++  java
  • vba中读取utf8格式的csv文件保证不乱码

    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
  • 相关阅读:
    HDOJ_2709_Sumsets
    HDOJ_2012_素数判定
    HDOJ_2011_大二写_水仙花树
    HDOJ_2010_大二写_水仙花数
    HDOJ_1290_大二写_献给杭电五十周年校庆的礼物
    HDOJ_2047_阿牛的EOF牛肉串
    HDOJ_2041_大二写_超级电梯
    HDOJ_2044_大二写_一只小蜜蜂...
    HDOJ_2046_骨牌方格_大二写
    HDOJ 2013_大二写
  • 原文地址:https://www.cnblogs.com/top5/p/1593990.html
Copyright © 2011-2022 走看看