0

我试图在一个看起来像这样的文件上使用这篇文章OpenText中描述的方法:

Ref,AccNum,SCode,RollNum
999,15697668,404040,4921817045040610

当我运行此代码时,第四列仍以科学记数法出现:

Workbooks.OpenText FileName:="H:\DataFile.csv", Origin:=3, StartRow:=1, _
        DataType:=1, TextQualifier:=1, ConsecutiveDelimiter:=False, _
        Tab:=False, Semicolon:=False, Comma:=True, Space:=False, _
        Other:=False, FieldInfo:=Array(Array(4, 2)), _
        TrailingMinusNumbers:=True

你知道我做错了什么吗?

4

4 回答 4

1

这有效:

FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 2))

似乎您无法删除Array()您不感兴趣的值。

于 2012-07-23T16:09:28.427 回答
0

Excel 会自动对大数字进行这些转换,但您可以通过以文本格式显示数字来解决它。
此示例显示语法:

ThisWorkbook.Sheets(1).Columns(4).NumberFormat = "@"
于 2012-07-23T15:16:47.080 回答
0

另一种方法是逐行读取 csv,将其加载到多维数组中,并在字符串前添加撇号。
我已经对此进行了测试,并且我很肯定它有效。

Sub ReadLines()

Dim oFSO                As FileSystemObject
Dim sLine               As String
Dim vArray()            As String
Dim vArray_Final        As Variant
Dim oTS                 As TextStream
Dim iCols               As Integer
Dim iRows               As Integer

Dim lCnt                As Long
Dim lCnt_X              As Long
Dim lCnt_Y              As Long

Set oFSO = New FileSystemObject
Set oTS = oFSO.OpenTextFile("U:\DataFile.csv")

lCnt_X = 1
lCnt_Y = 1

iCols = 4

Do Until oTS.AtEndOfStream
    sLine = oTS.ReadLine
    iRows = iRows + 1
Loop
Set oTS = Nothing

Set oTS = oFSO.OpenTextFile("U:\DataFile.csv")
ReDim vArray_Final(1 To iRows, 1 To iCols)

Do Until oTS.AtEndOfStream

    lCnt = lCnt + 1
    lCnt_X = 1
    lCnt_Y = 1

    sLine = oTS.ReadLine
    vArray = Split(sLine, ",")

    For lCnt_Y = 1 To iCols
        vArray_Final(lCnt, lCnt_Y) = vArray(lCnt_Y - 1)
    Next lCnt_Y

Loop

Set oTS = Nothing
Set oFSO = Nothing

For lCnt_X = 1 To iRows
    For lCnt_Y = 1 To iCols
        If lCnt_Y = iCols Then
            ThisWorkbook.Sheets(3).Cells(lCnt_X, lCnt_Y).Value = "'" & vArray_Final(lCnt_X, lCnt_Y)
        Else
            ThisWorkbook.Sheets(3).Cells(lCnt_X, lCnt_Y).Value = vArray_Final(lCnt_X, lCnt_Y)
        End If
    Next lCnt_Y
Next lCnt_X

End Sub
于 2012-07-25T12:36:31.150 回答
0

使用 FieldInfo 参数指定文本有效,除非它是 .csv,在这种情况下,您只需将扩展名更改为 .txt。出于某种原因,CSV 文件会忽略此类信息。但是该数组必须涵盖所有字段,而不仅仅是您要导入的字段(跳过是选项之一。)

于 2017-10-27T22:43:57.820 回答