VBA向csv文件添加行[关闭]

问题描述 投票:-2回答:2

我搜索了很多,但大多数结果都与excel有关。所以我无法找到答案的任何线索。

我试图通过ms access 2010:

我有几个带有标题的csv文件在第一行。所有字段都由分隔,并且它们都是字符串(双引号,它们在双引号中有很多)。

我不知道每个csv文件中有多少列。我想在第一行(标题)和第二行之间插入新行。

如何找出csv文件有多少列?如何在第二行添加新文本行?

excel vba csv access-vba ms-access-2010
2个回答
1
投票

FileSystemObject是执行此类任务的绝佳工具:

Public Function InsertLine()

    Const TmpExtension      As String = ".tmp"
    Const BakExtension      As String = ".bak"

    Dim FileSystemObject    As Scripting.FileSystemObject
    Dim SourceStream        As Scripting.TextStream
    Dim TargetStream        As Scripting.TextStream

    Dim Fields              As Variant
    Dim Values              As Variant
    Dim FieldCount          As Long
    Dim Item                As Long
    Dim Field               As String
    Dim FileName            As String
    Dim BaseName            As String
    Dim TempFileName        As String
    Dim Path                As String
    Dim Extension           As String
    Dim DotExtension        As String
    Dim TextLine            As String

    Set FileSystemObject = New Scripting.FileSystemObject

    ' File to modify.
    FileName = "c:\test\timezone.csv"

    BaseName = FileSystemObject.GetBaseName(FileName)
    Extension = FileSystemObject.GetExtensionName(FileName)
    Path = FileSystemObject.GetParentFolderName(FileName)
    DotExtension = "." & Extension
    TempFileName = BaseName & TmpExtension

    Set SourceStream = FileSystemObject.OpenTextFile(FileName, ForReading)
    Set TargetStream = FileSystemObject.CreateTextFile(TempFileName, ForWriting)

    ' Read header line.
    TextLine = SourceStream.ReadLine
    ' Copy header line.
    TargetStream.WriteLine TextLine

    ' Create line two.
    Fields = Split(TextLine, ",")
    ' FYI.
    FieldCount = UBound(Fields)
    ' Array to hold values.
    Values = Fields
    For Item = LBound(Fields) To UBound(Fields)
        Select Case Item
            Case 0
                Field = "SomeValue"
            Case 1
                Field = "OtherValue"
            Case 2
                Field = "YetAValue"
            ' etc.
            Case Else
                Field = CStr(Item)
        End Select
        Values(Item) = Chr(34) & Field & Chr(34)
    Next
    ' Write line two.
    TargetStream.WriteLine Join(Values, ",")

    ' Copy remaining lines.
    While Not SourceStream.AtEndOfStream
        TargetStream.WriteLine SourceStream.ReadLine
    Wend
    ' Close files.
    SourceStream.Close
    TargetStream.Close

    ' Rename files.
    FileSystemObject.MoveFile FileName, FileSystemObject.BuildPath(Path, BaseName & BakExtension)
    FileSystemObject.MoveFile TempFileName, FileSystemObject.BuildPath(Path, BaseName & DotExtension)

End Function

需要将引用设置为:Microsoft.Scripting.Runtime


-3
投票

只需录制宏即可为您完成工作。我做到了,我在下面得到了这段代码。

Sub Makro1()

Workbooks.Open Filename:="C:\Users\Excel\Desktop\Coding\R Programming\Merge Multiple CSV Files\CSV5.csv", Local:=True

Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

Range("A1").Select
ActiveCell.FormulaR1C1 = "field1"
Range("B1").Select
ActiveCell.FormulaR1C1 = "field2"
Range("C1").Select
ActiveCell.FormulaR1C1 = "field3"
Range("A1").Select
ActiveWorkbook.Save

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.