简体   繁体   中英

Trying to list files on excel spreadsheet VB.Net

I am trying to navigate to a folder using the Openfile Dialog command, then select some files. With these selected files, I am trying to write the name of those files onto a excel spreadsheet. I can navigate to the folder, but I can't get the files to write onto the spreadsheet.

I have the following code:

    Dim strFileName As String
    Dim openFile1 As OpenFileDialog = New OpenFileDialog()


    Dim Row As Integer
    Row = 14

    Const Column_A As Integer = 1
    Const Column_B As Integer = 2
    Const Column_C As Integer = 3
    Const Column_D As Integer = 4
    Const Column_E As Integer = 5
    Const Column_F As Integer = 6
    Const Column_U As Integer = 21

    openFile1.Title = "Open File Dialog"    'Title header for dialog box
    openFile1.InitialDirectory = "C:\" 'Initial directory for file dialog box
    openFile1.Filter = "All files (*.*)|" 'Filter all files
    openFile1.Multiselect = True 'Allows the user to select to select multiple files


    Dim Folder As IO.FileInfo()
    Dim FileName As IO.FileInfo

    If openFile1.ShowDialog() = DialogResult.OK Then 'This gives the "ok" to actually open the pop-up window
        For Each FileName In Folder.SelectedItems
            strFileName = openFile1.FileName  'Stores the Filename inside the variable
            Big_Sheet.Cells(Row, Column_B) = FileName
            Row = Row + 1
            Big_Sheet.Cells(Row, Column_C).Value = strFileName 'Shows the variable FilePath
        Next
    End If

A bit more research might be needed to write to an excel file like letting your program know that's what your doing.

Dim xls As Microsoft.Office.Interop.Excel.Application
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

xls = New Microsoft.Office.Interop.Excel.Application
xlsWorkBook = xls.Workbooks.Open("Insert file directory here")
xlsWorkSheet = xlsWorkBook.Sheets("sheet1")

xlsWorkSheet.Cells(1, 1) = TextBox1.Text

Hope you can use this to give you a bit better understanding, research is key my friend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM