我正在尝试使用“打开文件对话框”命令导航到文件夹,然后选择一些文件。使用这些选定的文件,我试图将它们写到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
希望您可以使用它来使您更好地理解,研究是我的朋友的关键。