【问题标题】:Connecting two path strings to get the final path?连接两个路径字符串以获得最终路径?
【发布时间】:2015-05-13 18:01:26
【问题描述】:

我正在尝试将 excel 文件保存到特定路径中。 所以基本上,当我单击按钮时,我正在创建一个文件夹,并希望将文件保存在该文件夹中。 创建的文件夹以当前月份为名称。我正在尝试保存到当前月份的文件夹中。

    'Create folder as Month Name. Save filename as date inside "month".
    Dim sDate As String = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")
    Dim sMonth As String = DateTime.Now.ToString("MMMM")
    Dim sFolder = Application.StartupPath & "\Resources\Excel\"


    My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))

    Dim sfinal = Path.Combine(sFolder, sMonth)
    xlSh.SaveAs(sfinal & Format(sDate) & ".xlsx")

    xlApp.Workbooks.Close()
    xlApp.Quit()

事实上,这段代码没有给我任何错误。但不是创建一个名为“March”

【问题讨论】:

    标签: excel excel-2010 vb.net-2010 vba


    【解决方案1】:

    您可以使用以下函数(类似于 .NET System.IO.Path.Combine)

    Function PathCombine(path1 As String, path2 As String)
    
        Dim combined As String
    
        combined = path1
        If Right$(path1, 1) <> Application.PathSeparator Then
            combined = combined & Application.PathSeparator
        End If
        combined = combined & path2
        PathCombine = combined
    
    End Function
    

    希望这会有所帮助!

    【讨论】:

    • 请注意,以上内容仅对 excel (问题被标记)有效,以防您最终在此页面上(像我一样)搜索 VBA 路径powerpoint 中的分隔符
    【解决方案2】:

    对于那些一直想知道我在做什么的人,这里是完整的子。如果有人需要类似的东西。感谢您的支持。问题已解决。

        Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
    
        Dim xlApp As Excel.Application
        Dim xlSh As Excel.Worksheet
    
    
        xlApp = New Excel.Application
    
    
        xlApp.Workbooks.Add()
        xlSh = xlApp.Workbooks(1).Worksheets(1)
    
        'Items from listbox1 to be exported into excel, second row, second column.
        Dim row As Integer = 2
        Dim col As Integer = 2
        For i As Integer = 0 To ListBox1.Items.Count - 1
            xlSh.Cells(row, col) = ListBox1.Items(i)
            row = row + 1
        Next
        row += 1
        col = 1
    
        'Items from listbox2 to be exported into excel, second row, third column.
        Dim row2 As Integer = 2
        Dim col2 As Integer = 3
        For i As Integer = 0 To ListBox2.Items.Count - 1
            xlSh.Cells(row2, col2) = ListBox2.Items(i)
            row2 = row2 + 1
        Next
        row2 += 1
        col2 = 1
    
    
        'Create folder as Month Name. Save filename as date inside that folder.
    
        'Make filename be yyyy-MM-DD_HH-mm-ss
        Dim sDate As String
        sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")
    
        'This will be used as name for the new folder.
        Dim sMonth As String
        sMonth = DateTime.Now.ToString("MMMM")
    
        'Filename + extension.
        Dim sFileName As String
        sFileName = sDate + ".xlsx"
    
        'This is the path.
        Dim sFolder As String
        sFolder = Application.StartupPath & "\Resources\Excel\"
    
        'This is the path combined with sMonth to make the final path.
        Dim sfinal As String
        sfinal = (sFolder & sMonth & "\")
    
        'Check if folder with the name sMonth already exists.
        If Dir(sFolder, vbDirectory) = sMonth Then
    
            'If it exist, then simply save the file inside the folder.
            xlSh.SaveAs(sfinal & Format(sFileName))
        Else
            'If it doesn't exist:
            'This is the creation of sMonth folder, inside "\excel\.
            My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))
            'This saves the excel file at path sfinal, with filename of sFileName
            xlSh.SaveAs(sfinal & Format(sFileName))
    
        End If
    
    
    
        'Close everything.
        xlApp.Workbooks.Close()
        xlApp.Quit()
    End Sub
    

    【讨论】:

      【解决方案3】:

      经过长时间的极度痛苦,我终于做到了! 显然我错过了一个“\” 由于“sMonth”成为动态名称,后来我想将其用作路径,并将文件保存在该文件夹中。我需要简单地将那个“\”放在 sMonth 之后,告诉它保存在里面。

      在我意识到这一点之前...我已经分解并尽可能简化了代码,以便我可以在逻辑上连接各个部分。我最终得到的结果略有不同。现在 SaveAS 将文件正确保存在新文件夹中。

          Dim sDate As String
          sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")
      
          Dim sMonth As String
          sMonth = DateTime.Now.ToString("MMMM")
      
          Dim sFileName As String
          sFileName = sDate + ".xlsx"
      
          Dim sFolder As String
          sFolder = Application.StartupPath & "\Resources\Excel\"
      
      
          Dim sfinal As String
          sfinal = (sFolder & sMonth & "\") '<- this thingie here o.O
      
          My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))
      
          xlSh.SaveAs(sfinal & Format(sFileName))
      
          xlApp.Workbooks.Close()
          xlApp.Quit()
      

      感谢您的帮助。

      【讨论】:

        【解决方案4】:

        您似乎实际上并未将保存路径设置为创建的目录。相反,我相信您将月份附加到 xlSh.SaveAs(sFinal & Format(sDate) & ".xlsx") 中文件名的开头。基本上(虽然我不确定具体的命令)你需要导航到你创建后创建的文件夹。 可能类似于

        的格式
        My.Computer.FileSystem.ChangeDirectory(sFolder & Format(sMonth))
        

        虽然我不知道该特定命令在我编写时确实存在。

        【讨论】:

        • 相信我,我已经尝试过...我在参考您发布的行时尝试了很多组合,但我无法让它工作...
        • 你能再次运行它并让它在使用每个变量时打印出每个变量的值吗?这样我们就可以看到输入到系统中的内容。
        • 不知道。我们确实知道问题出在哪里......这是这段代码: Dim sfinal = Path.Combine(sFolder, sMonth) xlSh.SaveAs(sfinal & Format(sDate) & ".xlsx") 我只是不知道其他另存为的方式
        • 很高兴你能成功!我正在询问输出,因为我想知道那个结尾的斜线,但听起来你自己得到了它,恭喜!
        • 哦,在另一件事上......如何检查目录是否存在只需保存文件,如果不存在,则创建文件夹并将文件保存在其中?
        猜你喜欢
        • 1970-01-01
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 2016-01-24
        • 2016-12-26
        • 2011-06-27
        相关资源
        最近更新 更多