excel 连接两个路径字符串以获得最终路径?

w6mmgewl  于 2023-01-27  发布在  其他
关注(0)|答案(5)|浏览(206)

我试图将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”〈-current month的文件夹并保存在其中,而是将文件保存在\Excel\中,并在同一位置创建文件夹。

qlfbtfca

qlfbtfca1#

您可以使用以下函数(类似于. 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

希望这有帮助!

4ioopgfo

4ioopgfo2#

经过长时间的痛苦折磨,我终于做到了!显然我错过了一个“\”,因为“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()

谢谢你的帮助。

clj7thdc

clj7thdc3#

你看起来并没有真正设置创建目录的保存路径。相反,我相信你在xlSh.SaveAs(sFinal & Format(sDate)&“.xlsx”)中将月份附加到文件名的开头。基本上(尽管我不确定具体的命令),你需要在创建文件夹后导航到它。* 可能 * 格式为

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

尽管我不知道我写的那个特定命令是否真的存在。

hof1towb

hof1towb4#

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

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
myzjeezk

myzjeezk5#

我发现这个方法容易得多。
创建一个FileSystemObject并使用BuildPath方法,如下所示:

Set fs = CreateObject("Scripting.FileSystemObject")
    
skPath = fs.BuildPath(ActiveDocument.Path, "Survival Story of Sword King")

收件人ActiveDocument.Path是Word中的当前目录,不能在Excel或其他中使用。对于Excel,它将是ActiveWorkbook.Path

我的观点是一些方法或命名空间是特定于应用程序的。

相关问题