[英]Trying to list files on excel spreadsheet VB.Net
我正在嘗試使用 Openfile Dialog 命令導航到一個文件夾,然后選擇一些文件。 使用這些選定的文件,我試圖將這些文件的名稱寫入 Excel 電子表格。 我可以導航到該文件夾,但無法將文件寫入電子表格。
我有以下代碼:
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
可能需要進行更多研究才能寫入 excel 文件,例如讓您的程序知道您在做什么。
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
希望你可以用它來讓你更好地理解,研究是我的朋友的關鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.