【问题标题】:reading and writing a csv file using FileSystemObject使用 FileSystemObject 读取和写入 csv 文件
【发布时间】:2012-03-15 13:55:04
【问题描述】:

是否可以在 VBA 中使用 FileSystemObject 读写 csv 文件?

【问题讨论】:

    标签: vba csv filesystemobject


    【解决方案1】:

    确实如此。

    基本语法如

        Set objFSO = CreateObject("scripting.filesystemobject")
        'create a csv file
        Set objTF = objFSO.createtextfile("C:\test\myfile.csv", True, False)
        'open an existing csv file with writing ability
        Set objTF = objFSO.OpenTextFile("C:\test\myfile.csv", 8) 
    

    将使用FSO 创建/打开CSV。

    CSV 然后可以通过写入来修改

    虽然这是一个 Excel 示例,但您可以使用相同的技术从 Outlook、Access、Word 等写入记录

    Const sFilePath = "C:\test\myfile.csv"
    Const strDelim = ","
    Sub CreateCSV_FSO()
        Dim objFSO
        Dim objTF
        Dim ws As Worksheet
        Dim lRow As Long
        Dim lCol As Long
        Dim strTmp As String
        Dim lFnum As Long
    
        Set objFSO = CreateObject("scripting.filesystemobject")
        Set objTF = objFSO.createtextfile(sFilePath, True, False)
    
        For Each ws In ActiveWorkbook.Worksheets
            'test that sheet has been used
            Set rng1 = ws.UsedRange
            If Not rng1 Is Nothing Then
                'only multi-cell ranges can be written to a 2D array
                If rng1.Cells.Count > 1 Then
                    X = ws.UsedRange.Value2
                    'The code TRANSPOSES COLUMNS AND ROWS by writing strings column by column
                    For lCol = 1 To UBound(X, 2)
                        'write initial value outside the loop
                        strTmp = IIf(InStr(X(1, lCol), strDelim) > 0, """" & X(1, lCol) & """", X(1, lCol))
                        For lRow = 2 To UBound(X, 1)
                            'concatenate long string & (short string with short string)
                            strTmp = strTmp & (strDelim & IIf(InStr(X(lRow, lCol), strDelim) > 0, """" & X(lRow, lCol) & """", X(lRow, lCol)))
                        Next lRow
                        'write each line to CSV
                        objTF.writeline strTmp
                    Next lCol
                Else
                    objTF.writeline IIf(InStr(ws.UsedRange.Value, strDelim) > 0, """" & ws.UsedRange.Value & """", ws.UsedRange.Value)
                End If
            End If
        Next ws
    
        objTF.Close
        Set objFSO = Nothing
        MsgBox "Done!", vbOKOnly
    
    End Sub
    

    【讨论】:

    • Set objTF = objFSO.OpenTextFile("C:\test\myfile.csv", 8) 无论我选择什么文件夹,该行总是会出现 70 - Permission Denied 错误。删除线路后它起作用了。
    • 不,那是我第一次尝试创建它的时候
    【解决方案2】:

    似乎有可能。

    FileSystemObject (FSO) 提供了一个 API 来访问 Windows 文件系统,提供对文件、驱动器、文本流等的访问。FSO 嵌入在 Microsoft 脚本运行时中,并且可用于独立应用程序(编码例如,使用 Visual Basic)、使用 VBScript 或 JScript 的网页设计人员以及使用 Visual Basic for Applications (VBA) 的 Microsoft Office 应用程序用户。

    一些参考资料:-

    Using The FileSystemObject With VB and VBA

    How do I use FileSystemObject in VBA?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2022-01-23
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多