0

如何通过 Excel VBA 以一组、组或多个单个文件的形式导入 CSV 文件,而不是一次一个?

4

6 回答 6

2

我有点困惑,大多数版本的 Excel 都可以毫无问题地打开 .csv 文件。

strPath = "C:\Docs\"
strFile = Dir(strPath & "*.csv")

Do While strFile <> ""
    Workbooks.Open Filename:=strPath & strFile
    ActiveWorkbook.SaveAs Filename:=strPath & Mid(strFile, 1, InStr(strFile, ".") - 1) _
        & "Conv.xls", FileFormat:=xlNormal

    strFile = Dir
Loop
于 2008-10-23T21:41:42.203 回答
1

这将快速将文件放入一维数组

Open "myfile.csv" For Input As 1
Dim Txt As String
Txt = Input(LOF(1), 1)
Close #1
Dim V As Variant
V = Split(Txt, ",")

然后V包含单个列中的所有项目,NB第一项是V(0)

于 2008-10-23T08:36:26.920 回答
1

这是另一种避免在 Excel 中打开 csv 文件时重新计算的方法。

将一张空白工作表添加到您的工作簿并在工作表的对象中添加以下代码

Function getCsv(fn)
    Dim d, scrup As Boolean
    scrup = Application.ScreenUpdating
    Application.ScreenUpdating = False
    With Me.QueryTables.Add( _
            Connection:="TEXT;" & fn, _
            Destination:=Me.Range("A1") _
        )
        .Name = "data"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileCommaDelimiter = True
        .Refresh BackgroundQuery:=False
    End With

    d = Me.Names(1).RefersToRange.Value

    Me.Names(1).Delete
    Me.UsedRange.Rows.Delete

    Application.ScreenUpdating = scrup
    getCsv = d
End Function

您可能还想隐藏工作表,以免有人意外使用它。然后你可以像这样使用

dim d
d = getCsv("C:\temp\some.csv")

让我稍微担心的一件事是,每次使用函数时,名称都会增加(例如 data_1、data_2、...),因此如果这些名称保存在某个地方,可能会出现稳定性问题。

于 2011-06-17T11:15:18.020 回答
0

你也可以使用 workbooks.opentext

于 2008-10-25T00:38:12.957 回答
0

您可以编写一个简单的控制台应用程序来解析一批 csv 文件并将它们保存到 excel 文件中。
不是最简单的解决方案,但它可能是一种选择。

于 2008-10-23T06:26:30.630 回答
0

我最初的想法如下。鉴于此数据

Dog Names,Dog Ages,Collar Size
Woof,3,4
Bowser,2,5
Ruffy,4.5,6
Angel,1,7
Demon,7,8
Dog,9,2

创建三个名为 和 的全局数组,Dog_NamesDog_AgesCollar_SizeCSV 文件中的数据填充它们。

这部分 VBScript 完成了这项工作并显示了结果。wscript.echo从子例程中删除注释标记x以查看这一切。

Option Explicit

Dim FSO
Set FSO = CreateObject( "Scripting.FileSystemObject" )

Dim oStream
Dim sData
Dim aData

Set oStream = fso.OpenTextFile("data.csv")
sData       = oStream.ReadAll
aData       = Split( sData, vbNewLine )

Dim sLine
sLine = aData(0)

Dim aContent
aContent = Split( sLine, "," )

Dim aNames()
Dim nArrayCount
nArrayCount = UBound( aContent )
ReDim aNames( nArrayCount )

Dim i

For i = 0 To nArrayCount
  aNames(i) = Replace( aContent( i ), " ", "_" )
  x "dim " & aNames(i) & "()"
Next

For j = 0 To nArrayCount
  x "redim " & aNames(j) & "( " & UBound( aData ) - 1 & " )"
Next

Dim j
Dim actual
actual = 0

For i = 1 To UBound( aData )
  sLine = aData( i )
  If sLine <> vbnullstring Then
    actual = actual + 1
    aContent = Split( sLine, "," )
    For j = 0 To nArrayCount
      x aNames(j) & "(" & i - 1 & ")=" & Chr(34) & aContent(j) & Chr(34)
    Next
  End If
Next

For j = 0 To nArrayCount
  x "redim preserve " & aNames(j) & "(" & actual - 1 & ")"
Next

For i = 0 To actual - 1
  For j = 0 To nArrayCount
    x "wscript.echo aNames(" & j & ")," & aNames(j) & "(" & i & ")"
  Next
Next

Sub x( s )
  'wscript.echo s
  executeglobal s
End Sub

结果看起来像这样

>cscript "C:\Documents and Settings\Bruce\Desktop\datathing.vbs"
Dog_Names Woof
Dog_Ages 3
Collar_Size 4
Dog_Names Bowser
Dog_Ages 2
Collar_Size 5
Dog_Names Ruffy
Dog_Ages 4.5
Collar_Size 6
Dog_Names Angel
Dog_Ages 1
Collar_Size 7
Dog_Names Demon
Dog_Ages 7
Collar_Size 8
Dog_Names Dog
Dog_Ages 9
Collar_Size 2
>Exit code: 0    Time: 0.338
于 2008-10-29T02:59:06.437 回答