使用VB.NET进行递归Excel操作

汤姆·钱伯斯

我正在使用Visual Studio 2015和VB.net,并且我有2个文件夹。
1. C:\ phd \ unclean 2. C:\ phd \ clean

在不干净的文件夹中。我有各种包含各种文件的子文件夹和子文件夹。我要在所有unclean的子文件夹和子文件夹中获取所有.csv文件,对其进行处理以对其进行清理,然后使用与在unclean中相同的子文件夹结构将它们输出到C:\ clean BUT中。

到目前为止,这是我的代码...

    Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Class MainWindow
    Dim xl As Excel.Application = New Excel.ApplicationClass()
    Dim wb, wbTraj, wbForce As Excel.Workbook
    Dim ws, wsData, wsLeg As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim iCol As Integer
    Dim iRow As Integer
    Dim trajEndRow, analogEndRow, analogDataRow, forceLegRowStart, forceLegRowEnd, forceDataRow, lastDataRow As Integer
    Dim cell, trajDataRangeSrc, trajDataRangeDest, trajLegSrc, trajLegDest, analogDataRange, forceDataRangeSrc, forceDataRangeDest, forceLegSrc, forceLegDest As Excel.Range
    Dim strName As String
    Dim blank As String
    Dim iIndex As Integer
    Dim strPath As String
    Dim strFile As String

    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        If cleanRadioButton.IsChecked = True Then
            Dim list As List(Of String) = GetFilesRecursive("C:\phd\unclean")

            ' Loop through and display each path.
            For Each path In list
                clean(path)
            Next

        Else inputRadioButton.IsChecked = True
            ' do something else
        End If
        releaseObject(ws)
        releaseObject(wsData)
        releaseObject(wsLeg)
        releaseObject(wb)
        releaseObject(wbForce)
        releaseObject(wbTraj)
        releaseObject(xl)
    End Sub

Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)
        ' This list stores the results.
        Dim result As New List(Of String)

        ' This stack stores the directories to process.
        Dim stack As New Stack(Of String)

        ' Add the initial directory
        stack.Push(initial)

        ' Continue processing for each stacked directory
        Do While (stack.Count > 0)
            ' Get top directory string
            Dim dir As String = stack.Pop
            Try
                ' Add all immediate file paths
                result.AddRange(Directory.GetFiles(dir, "*.csv"))

                ' Loop through all subdirectories and add them to the stack.
                Dim directoryName As String
                For Each directoryName In Directory.GetDirectories(dir)
                    stack.Push(directoryName)
                Next

            Catch ex As Exception
            End Try
        Loop

        ' Return the list
        Return result
    End Function

Private Sub clean(path)
        strPath = path
        strFile = Dir(strPath & "*.csv")
        Do While strFile <> ""
            wb = xl.Workbooks.Open(Filename:=strPath & strFile)

            'Loop through the sheets.
            For iIndex = 1 To xl.ActiveWorkbook.Worksheets.Count
                ws = xl.ActiveWorkbook.Worksheets(iIndex)

                'Loop through the columns.
                For iCol = 1 To ws.UsedRange.Columns.Count
                    'Check row 10 of this column for the char of *
                    If InStr(ws.Cells(10, iCol).Value, "*") > 0 Then
                        'We have found a column with the char of *
                        xl.DisplayAlerts = False
                        ws.Columns(iCol).EntireColumn.Delete
                        ws.Columns(iCol).EntireColumn.Delete
                        ws.Columns(iCol).EntireColumn.Delete
                        iCol = iCol - 3
                    End If
                Next iCol

            Next iIndex
            wb.SaveAs(Filename:="C:\phd\clean\" & wb.Name, FileFormat:=51)
            wb.Close(SaveChanges:=False)
            strFile = Dir()
        Loop
        MessageBox.Show("The csv files have now been cleaned.  Congrats.")
    End Sub

但是,我无法正常工作,我迷失了自己。谁能帮我浏览结构,找到任何.csv文件,清理它,然后将其输出到clean文件夹下的相同文件结构中,然后继续搜索下一个.csv文件?

令人困惑

谢谢

普萨·庞杰伦德拉普

如果要克隆文件夹结构,仅保存目录名称似乎还不够。您还需要知道每个文件夹中有哪些CSV。为此,我将保存一个List(Of FileInfo)同时包含CSV文件名及其原始文件夹的文件。收集它们:

Private myCSVList As List(Of FileInfo)

Private Sub Button_Click(sender As Object, 
      e As EventArgs) Handles Button29.Click
    myCSVList = New List(Of FileInfo)

    FindCSVs("C:\Temp")
    ' print some:
    For n As Int32 = 0 To myCSVList.Count - 1 Step 2
        Console.WriteLine(myCSVList(n).FullName)
    Next
End Sub

Private Sub FindCSVs(path As String)
    Dim di As New DirectoryInfo(path)
    ' add the csvs in THIS folder
    myCSVList.AddRange(di.EnumerateFiles("*.csv").ToArray)

    ' look for csvs in sub folders
    For Each d As DirectoryInfo In di.EnumerateDirectories
        FindCSVs(d.FullName)
    Next
End Sub

输出:

C:\ Temp \ capitals.csv
C:\ Temp \ mycsv.csv
C:\ Temp \ townsinfo.csv
C:\ Temp \ A \ AA \ capitals.csv
C:\ Temp \ A \ AA \ AAA \ AAAA \ capitals .csv
C:\ Temp \ B \ BB \ capitals.csv

现在,您已拥有所有CSV的待办事项列表,对其进行处理并将其写回到新文件夹中。您应该可以String.Replace在存储的路径上使用来更改C:\phd\uncleanC:\phd\clean我将包括驱动器部分,以仅更改第一个外观,以防“ clean”出现在路径中的其他地方。

如果您需要对初始列表进行更广泛的处理,请根据日期或名称等排除某些内容,我可以使用其他方法Sub

...
' add the csvs in this folder
myCSVList.AddRange(LoadFiles(di))

Private Function LoadFiles(di As DirectoryInfo) As FileInfo()
    Dim thisFolder = di.EnumerateFiles("*.csv").ToList
    ' ...do stuff to remove unqualified ones
    ' ...
    Return thisFolder.ToArray

End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章